Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下代码将 str 设置为“testss”
String str = "test".replaceAll("(.*)$","$1s");
以下代码将其设置为“测试”
String str = "test".replaceFirst("(.*)$","$1s");
我原以为这两种操作都会产生相同的结果。有人可以解释为什么 replaceAll 在字符串末尾添加一个额外的 s 吗?
这是因为"(.*)$"从 中捕获两个字符串"test","test"以及空字符串 ("")。所以 replaceAll 将添加两个"s".
"(.*)$"
"test"
"s"