提交 1870e863 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Handle empty before argument in StringUtils.replaceAll()

上级 8f6a5b14
...@@ -707,7 +707,10 @@ public class StringUtils { ...@@ -707,7 +707,10 @@ public class StringUtils {
} }
/** /**
* Replace all occurrences of the before string with the after string. * Replace all occurrences of the before string with the after string. Unlike
* {@link String#replaceAll(String, String)} this method reads {@code before}
* and {@code after} arguments as plain strings and if {@code before} argument
* is an empty string this method returns original string {@code s}.
* *
* @param s the string * @param s the string
* @param before the old text * @param before the old text
...@@ -716,7 +719,7 @@ public class StringUtils { ...@@ -716,7 +719,7 @@ public class StringUtils {
*/ */
public static String replaceAll(String s, String before, String after) { public static String replaceAll(String s, String before, String after) {
int next = s.indexOf(before); int next = s.indexOf(before);
if (next < 0) { if (next < 0 || before.isEmpty()) {
return s; return s;
} }
StringBuilder buff = new StringBuilder( StringBuilder buff = new StringBuilder(
......
...@@ -225,6 +225,8 @@ public class TestStringUtils extends TestBase { ...@@ -225,6 +225,8 @@ public class TestStringUtils extends TestBase {
StringUtils.replaceAll("abcabcabc", "abc", "")); StringUtils.replaceAll("abcabcabc", "abc", ""));
assertEquals("abcabcabc", assertEquals("abcabcabc",
StringUtils.replaceAll("abcabcabc", "aBc", "")); StringUtils.replaceAll("abcabcabc", "aBc", ""));
assertEquals("abcabcabc",
StringUtils.replaceAll("abcabcabc", "", "abc"));
} }
private void testTrim() { private void testTrim() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论