[LANG-1033] Add StringUtils.countMatches(CharSequence, char)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1616372 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a67075b783
commit
1384457a39
|
@ -22,6 +22,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="3.4" date="tba" description="tba">
|
<release version="3.4" date="tba" description="tba">
|
||||||
|
<action issue="LANG-1033" type="add" dev="ggregory">Add StringUtils.countMatches(CharSequence, char)</action>
|
||||||
<action issue="LANG-1027" type="update" dev="rmannibucau">org.apache.commons.lang3.SystemUtils#isJavaVersionAtLeast should return true by default</action>
|
<action issue="LANG-1027" type="update" dev="rmannibucau">org.apache.commons.lang3.SystemUtils#isJavaVersionAtLeast should return true by default</action>
|
||||||
<action issue="LANG-1021" type="add" dev="britter" due-to="Alexander Müller">Provide methods to retrieve all fields/methods annotated with a specific type</action>
|
<action issue="LANG-1021" type="add" dev="britter" due-to="Alexander Müller">Provide methods to retrieve all fields/methods annotated with a specific type</action>
|
||||||
<action issue="LANG-1026" type="update" dev="britter" due-to="Alex Yursha">Bring static method references in StringUtils to consistent style</action>
|
<action issue="LANG-1026" type="update" dev="britter" due-to="Alex Yursha">Bring static method references in StringUtils to consistent style</action>
|
||||||
|
|
|
@ -5822,6 +5822,39 @@ public class StringUtils {
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Counts how many times the char appears in the given string.</p>
|
||||||
|
*
|
||||||
|
* <p>A {@code null} or empty ("") String input returns {@code 0}.</p>
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* StringUtils.countMatches(null, *) = 0
|
||||||
|
* StringUtils.countMatches("", *) = 0
|
||||||
|
* StringUtils.countMatches("abba", 0) = 0
|
||||||
|
* StringUtils.countMatches("abba", 'a') = 2
|
||||||
|
* StringUtils.countMatches("abba", 'b') = 2
|
||||||
|
* StringUtils.countMatches("abba", 'x') = 0
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param str the CharSequence to check, may be null
|
||||||
|
* @param ch the char to count
|
||||||
|
* @return the number of occurrences, 0 if the CharSequence is {@code null}
|
||||||
|
* @since 3.4
|
||||||
|
*/
|
||||||
|
public static int countMatches(final CharSequence str, final char ch) {
|
||||||
|
if (isEmpty(str)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
// We could also call str.toCharArray() for faster look ups but that would generate more garbage.
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
if (ch == str.charAt(i)) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
// Character Tests
|
// Character Tests
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -322,4 +322,12 @@ public class StringUtilsSubstringTest {
|
||||||
StringUtils.countMatches("oooooooooooo", "ooo"));
|
StringUtils.countMatches("oooooooooooo", "ooo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCountMatches_char() {
|
||||||
|
assertEquals(0, StringUtils.countMatches(null, 'D'));
|
||||||
|
assertEquals(5, StringUtils.countMatches("one long someone sentence of one", ' '));
|
||||||
|
assertEquals(6, StringUtils.countMatches("one long someone sentence of one", 'o'));
|
||||||
|
assertEquals(4, StringUtils.countMatches("oooooooooooo", "ooo"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue