diff --git a/src/changes/changes.xml b/src/changes/changes.xml index f876ca4e6..25d0ee02b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,7 @@ The type attribute can be add,update,fix,remove. Restore handling of collections for non-JSON ToStringStyle #610. ContextedException Javadoc add missing semicolon #581. + StringUtils.countMatches - clarify Javadoc. Remove redundant argument from substring call. Improve StringUtils.stripAccents conversion of remaining accents. ArrayUtils.toPrimitive(Object) does not support boolean and other types #607. diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index a93d34a7f..0949398ad 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -1425,7 +1425,8 @@ public class StringUtils { // Count matches //----------------------------------------------------------------------- /** - *

Counts how many times the substring appears in the larger string.

+ *

Counts how many times the substring appears in the larger string. + * Note that the code only counts non-overlapping matches.

* *

A {@code null} or empty ("") String input returns {@code 0}.

* @@ -1437,6 +1438,7 @@ public class StringUtils { * StringUtils.countMatches("abba", "a") = 2 * StringUtils.countMatches("abba", "ab") = 1 * StringUtils.countMatches("abba", "xxx") = 0 + * StringUtils.countMatches("ababa", "aba") = 1 * * * @param str the CharSequence to check, may be null diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java index 9a1c014fe..d876d7c94 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsSubstringTest.java @@ -349,6 +349,14 @@ public class StringUtilsSubstringTest { StringUtils.countMatches("one long someone sentence of one", "two")); assertEquals(4, StringUtils.countMatches("oooooooooooo", "ooo")); + assertEquals(0, StringUtils.countMatches(null, "?")); + assertEquals(0, StringUtils.countMatches("", "?")); + assertEquals(0, StringUtils.countMatches("abba", null)); + assertEquals(0, StringUtils.countMatches("abba", "")); + assertEquals(2, StringUtils.countMatches("abba", "a")); + assertEquals(1, StringUtils.countMatches("abba", "ab")); + assertEquals(0, StringUtils.countMatches("abba", "xxx")); + assertEquals(1, StringUtils.countMatches("ababa", "aba")); } @Test