4

I have a simple problem and yet to find the optimal solution.

Imagine I have a url with a query param named 'id' like any of the following formats

  • xxxxxxxxxxxxx?id=123
  • xxxxxxxxxxxxx?id=123&xxxxxxxx
  • xxxxxxxxxxxxx?xxxxxx&id=123
  • xxxxxxxxxxxxx?xxxxxx&id=123&xxxxx

I'm taking the above string (could be any one above) as a String parameter and want to figure out the simplest way to remove the query parameter id (name and value), so that even after removing, it will be a valid url.

Below are my expected outputs (in order).

  • xxxxxxxxxxxxx
  • xxxxxxxxxxxxx?xxxxxxxx
  • xxxxxxxxxxxxx?xxxxxx
  • xxxxxxxxxxxxx?xxxxxx&xxxxx

Can anyone have a good idea? (only with String operations, without any util classes)

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87

4 Answers4

5

You can use replaceAll which is using regex like so :

String[] urls = new String[]{
    "xxxxxxxxxxxxx?id=123", "xxxxxxxxxxxxx?id=123&xxxxxxxx", 
    "xxxxxxxxxxxxx?xxxxxx&id=123", "xxxxxxxxxxxxx?xxxxxx&id=123&xxxxx"
};
for (String url : urls) {
    System.out.println(
            url.replaceAll("([\\?&]id=\\d+$)|(id=\\d+&)", "")
    );
}

Output

xxxxxxxxxxxxx
xxxxxxxxxxxxx?xxxxxxxx
xxxxxxxxxxxxx?xxxxxx
xxxxxxxxxxxxx?xxxxxx&xxxxx

Demo

Regex demo


Details

this regex ([\?&]id=\d+$)|(id=\d+&) mean to match :

  • ([\?&]id=\d+$) first group
    • [\?&] match literal ? or & character
    • id followed by literal id word
    • \d+ followed by one or mode digit
    • $ end of line
  • | or
  • (id=\d+&) match group two
    • id match literal id word
    • \d+ followed by one or mode digit
    • & match literal & character

If you the input after id= can be other than a digit you can use \w instead which match \w word character which can match any character in [A-Za-z0-9_]

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

Another approach without regex:

String removeParameter(String url, String paramToRemove) {
    String[] parts = url.split("\\?");
    if (parts.length == 2) {
        String base = parts[0];
        String params = parts[1];

        return base + "?" + Stream.of(params.split("&"))
                .map(p -> p.split("="))
                .filter(p -> !p[0].equals(paramToRemove))
                .map(p -> String.join("=", p))
                .collect(Collectors.joining("&"));
    } else {
        return url;
    }
}

And use it like this:

removeParameter("xxxxxxxxxxxxx?xxxxxx&id=123", "id") // xxxxxxxxxxxxx?xxxxxx
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
0

Below code works for me. Hope this helps.

public class A {

    public static void main(String[] args) {
        String s = "xxxxxxxxxxxxx?id=123";
        String s1 = "xxxxxxxxxxxxx?id=123&xxxxxxxx";
        s = getIdRemovedString(s);
        s1 = getIdRemovedString(s1);
        System.out.println(s);
        System.out.println(s1);
    }

    private static String getIdRemovedString(String s) {
        int startIndex = s.indexOf("?id") - 1;
        int endIndex = s.indexOf("&", s.indexOf("?id"));
        String idSubString = s.substring(startIndex, endIndex == -1 ? s.length() : endIndex);
        return s.replace(idSubString, "");
    }

}

PS: please ignore static.

0

You can try below code:

String finalStr =request.getRequestURL()+"?";
String params[] = request.getQueryString().split("&");
for (String p : params) {
    String values[] = p.split("=");
    if(values.length == 2) {
        finalStr= finalStr + values[1]+"&";
    }else {
        finalStr= finalStr + values[0]+"&";
    }
}
System.out.println("output"+finalStr.substring(0, finalStr.length()-1));
AmitD
  • 11
  • 6