Removing / processing strings in Java

String url = "http://someurl/search.do?/remainingurl"

I would like to clear the specified string from the first character to search.do?

The value of the resulting string will look like this:

String scrapedUrl = "http://someurl/search.do?"

Is there a String operation in Java to perform the above action?

Thanks Sony

+3
source share
8 answers

Use regex:

String scrapedUrl = url.replaceAll("(.*?search\\.do\\?).*", "$1");

Of course, you can use solutions indexOfand all this, but your requirement was to precisely "combine" everything to the first search.do?. Such an operation is best performed using a regular expression.

+4
source

Try this one (untested):

int index = url.indexOf("?");
if (index >= 0)
    url= url.substring(0, index);

He is looking for the first appearance of '?' character, and if found chopped off the characters that follow.

+1
source
0
final String splitter = "search.do?";
String[] strings = url.split("search\\.do\\?");
String scrapedUrl = strings[0] + splitter;

Read more .

0
source
    String url = "http://someurl/search.do?/remainingurl";
   String scrapedUrl =url.substring(0,url.lastIndexOf("?")+1);
   System.out.println(scrapedUrl);
0
source

I would recommend:

int index = url.indexOf("?");
String newURL = url.subString(0,index);
0
source

You can use substring and indexOf

String scrapedUrl = url.substring(0, url.indexOf('?');
0
source

All Articles