更新时间:2022-04-05 05:36:39
您需要使用 replaceAll()
方法的返回值。 replaceAll()
不替换当前字符串中的字符,它返回一个带替换字符的新字符串。
You need to use return value of replaceAll()
method. replaceAll()
does not replace the characters in the current string, it returns a new string with replacement.
- 字符串对象是不可变的,它们的值在创建后无法更改。
- 您可以使用replace()而不是replaceAll()如果你不需要正则表达式。
String str = "abcd=0; efgh=1";
String replacedStr = str.replaceAll("abcd", "dddd");
System.out.println(str);
System.out.println(replacedStr);
输出
abcd=0; efgh=1
dddd=0; efgh=1