我经常replace(CharSequence,CharSequence)
在Java中使用该方法,但是今天遇到了一些我不太了解的东西。
当我做 :
"|||".replace("||","| |");
为什么它会导致:| ||
而不是| | |
?为什么第二个|
不能包含在两个模式中?
为了解决我的问题,我必须写"|||".replace("||","| |").replace("||","| |");
这看起来有点多余。
任何人都有解释来帮助我更好地理解背后的机制?
编辑
所以有人说这是javadoc中指定的特殊情况,但是错误的例子如下:
* The replacement proceeds from the beginning of the string to the end, for
* example, replacing "aa" with "b" in the string "aaa" will result in
* "ba" rather than "ab".
所以它与我的情况不同,因为结果“ba”不再匹配“aa”。而在我的情况下,结果“| ||” 仍然包含模式。