ArrayOutOfBoundsException
使用时我感到很奇怪replaceFirst
:
"this is an example string".replaceFirst("(^this )", "$1\\") // throws ArrayOutOfBoundsException
"this is an example string".replaceFirst("(^this )", "$1") // works fine
我正在尝试实现这个字符串:
"this \is an example string"
如果我尝试在替换字符串中放入转义的反斜杠,为什么会收到 ArrayOutOfBoundsException?这发生在Android上,如果它有所作为
这是异常的一个ideone示例。
这是 logcat 异常堆栈跟踪:
java.lang.ArrayIndexOutOfBoundsException: index=14 at java.util.regex.Matcher.appendEvaluated(Matcher.java:149) at java.util.regex.Matcher.appendReplacement(Matcher.java:111) at java.util.regex。 Matcher.replaceFirst(Matcher.java:304) 在 java.lang.String.replaceFirst(String.java:1793)