【发布时间】:2018-10-28 13:32:08
【问题描述】:
我有一个类似的网址:http://example.com:8080/files/username/oldpassword/12351.png
我需要将旧密码替换为:新密码。
oldpassword 不是固定字符串,它是未知字符串。
目前我使用这个代码:
String url = "http://example.com:8080/files/username/oldpassword/12351.png";
String[] split = url.split("/");
String oldPass = split[5];
String newPass = "anyNewRandomPassword";
if( !oldPass.equals(newPass)) {
url = url.replace(oldPass, newPass);
}
我认为可以使用正则表达式来完成。
非常感谢任何帮助。
【问题讨论】:
-
这行得通吗?
-
在内置 replace() 中使用
-
是的,我附加的这个代码是有效的。