出于某种原因,我得到了这个输出
通行证: 12345
港口: 8080
曲奇:找不到
宿主:本地主机
密码: 12345
当我想
通行证: 12345
港口: 8080
曲奇:找不到
宿主:本地主机
密码: 12345
public static String modifyString(String str) {
if (str.matches("(.*)=($)")){
str = str.replace("=", " : not found");
} else {
str = str.replace("=", " : ");
}
return str;
}
if (arr[i] != null){
if (arr[i].contains("pass")) {
arr[arr.length - 1] = arr[i];
arr[arr.length -1] = arr[i].replaceAll("pass", "password");
System.out.println(modifyString(arr[i]));
} else {
System.out.println(modifyString(arr[i]));
}发布于 2020-06-17 20:16:03
您试图处理字符串“密码”。replaceAll(“pass”、“password”)将“pass”替换为“password”,并保留“word”。因此,结果肯定是“密码”。
发布于 2020-06-18 07:41:35
因为"passwordword". (“pass”,“replaceAll”)与"password“匹配
要解决这个问题,一个简单的解决方案是使用正则表达式:
string.replaceAll("\\bpass\\b", "password");这将导致它只匹配单词"pass“,而不执行部分匹配。
有关这一点的更多文档可以在Javadocs的模式类中找到,或者有关更多的示例,请参阅本文:
https://howtodoinjava.com/regex/java-regex-specific-contain-word/
https://stackoverflow.com/questions/62437488
复制相似问题