This is an extension of the answer provided by mchr.
It allows params to be removed from an url and from a query string, and shows how to execute the javascript test cases he mentions. Since it uses a regex, it will return the url with all the other parameters exactly where they were before. This is useful if you want to remove some parameters from an url when "signing" an url ;-) Note that this does not remove any completely empty parameters.
e.g. it will not remove foo
from /test?foo&me=52
The test cases listed below remove the parameters, "foo" and "test" when found in the query string.
class Main {
public static void main(String[] args) {
runTests();
}
public static void runTests() {
test("foo=%2F{}/me/you&me=52", "me=52");
test("?foo=%2F{}/me/you&me=52", "?me=52");
test("?foo=52&me=able was i ere&test=2", "?me=able was i ere");
test("foo=", "");
test("?", "");
test("?foo=52", "");
test("test?", "test");
test("test?foo=23", "test");
test("foo=&bar=456", "bar=456");
test("bar=456&foo=", "bar=456");
test("abc=789&foo=&bar=456", "abc=789&bar=456");
test("foo=123", "");
test("foo=123&bar=456", "bar=456");
test("bar=456&foo=123", "bar=456");
test("abc=789&foo=123&bar=456", "abc=789&bar=456");
test("xfoo", "xfoo");
test("xfoo&bar=456", "xfoo&bar=456");
test("bar=456&xfoo", "bar=456&xfoo");
test("abc=789&xfoo&bar=456", "abc=789&xfoo&bar=456");
test("xfoo=", "xfoo=");
test("xfoo=&bar=456", "xfoo=&bar=456");
test("bar=456&xfoo=", "bar=456&xfoo=");
test("abc=789&xfoo=&bar=456", "abc=789&xfoo=&bar=456");
test("xfoo=123", "xfoo=123");
test("xfoo=123&bar=456", "xfoo=123&bar=456");
test("bar=456&xfoo=123", "bar=456&xfoo=123");
test("abc=789&xfoo=123&bar=456", "abc=789&xfoo=123&bar=456");
test("foox", "foox");
test("foox&bar=456", "foox&bar=456");
test("bar=456&foox", "bar=456&foox");
test("abc=789&foox&bar=456", "abc=789&foox&bar=456");
test("foox=", "foox=");
test("foox=&bar=456", "foox=&bar=456");
test("bar=456&foox=", "bar=456&foox=");
test("abc=789&foox=&bar=456", "abc=789&foox=&bar=456");
test("foox=123", "foox=123");
test("foox=123&bar=456", "foox=123&bar=456");
test("bar=456&foox=123", "bar=456&foox=123");
test("abc=789&foox=123&bar=456", "abc=789&foox=123&bar=456");
}
public static void test (String input, String expected) {
String result = removeParamsFromUrl(input, "foo", "test");
if (! result.equals(expected))
throw new RuntimeException("Failed:" + input);
System.out.println("Passed:" + input + ", output:" + result);
}
public static String removeParamsFromQueryString(String queryString, String... params) {
for (String param : params) {
String keyValue = param + "=[^&]*?";
queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
}
return queryString;
}
public static String removeParamsFromUrl(String url, String... params) {
String queryString;
String baseUrl;
int index = url.indexOf("?");
boolean wasFullUrl = (index != -1);
if (wasFullUrl)
{
baseUrl = url.substring(0, index);
queryString = url.substring(index+1);
}
else
{
baseUrl = "";
queryString = url;
}
String newQueryString = removeParamsFromQueryString(queryString, params);
String result;
if (wasFullUrl)
{
boolean isEmpty = newQueryString == null || newQueryString.equals("");
result = isEmpty ? baseUrl : baseUrl + "?" + newQueryString;
}
else
{
result = newQueryString;
}
return result;
}
}