假设我有一个像 token=1234235asdjaklj231k209a&name=sam&firname=Mahan 这样的 url 参数
如何用新的 token 替换 token 的值?
我以前用模式和匹配器做过类似的事情,但现在不记得了
但我知道有办法做到这一点
更新: token 可以包含除 & 之外的任何字母
提前致谢
最佳答案
Spring 有一个实用程序可以优雅地处理这种需求。 Apache httpcomponents 也是如此。下面是一个 Spring 示例。
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
public class StackOverflow {
private static class SO46303058 {
public static void main(String[] args) {
final String urlString = "https://subdomain.hostname/path/resource?token=1234235asdjaklj231k209a&name=sam&firname=Mahan";
final URI uri = UriComponentsBuilder.fromHttpUrl(urlString)
.replaceQueryParam("token", "abc")
.build().toUri();
System.out.println(uri);
}
}
}
不要害怕向项目添加依赖项,这比重新发明轮子要好。
关于java - 替换 Java 中 QueryString 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46303058/