移除url上特定的参数
private static String removeParam(String url, String... name) {
for (String s : name) {
// 使用replaceAll正则替换,replace不支持正则
url = url.replaceAll("&&?" + s + "=[^&]*", "");
}
return url;
}
测试用例
public static void main(String[] args) {
String url =
"https://www.lyf.com/user/info?uid=1&enc=88182&platform=1001&t=1597372964477&id=123";
System.out.println(url);
String string = removeParam(url, "platform", "id");
// 结果: https://www.lyf.com/user/info?uid=1&t=1597372964477
System.out.println(string);
}
输出结果:
https://www.lyf.com/user/info?uid=1&enc=88182&platform=1001&t=1597372964477&id=123
https://www.lyf.com/user/info?uid=1&enc=88182&t=1597372964477