String url = "http://someurl/search.do?/remainingurl"
I would like to scrape the above string from the first character upto search.do?
Meaning, the resulting String would look like so:
String scrapedUrl = "http://someurl/search.do?"
Is there a String operation in Java to achieve the above action ?
Thanks, Sony
Use a regular expression:
String scrapedUrl = url.replaceAll("(.*?search\\.do\\?).*", "$1");
Of course you can use indexOf solutions and all of that, but your requirement was to exactly ''match'' everything up until the first search.do?. That kind of operation is best done using a regular expression.
Try this (untested):
int index = url.indexOf("?");
if (index >= 0)
url= url.substring(0, index);
It looks for the first occurance of the '?' character and if found chops off the characters that follow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With