Java Utililty Methods String Match Count

List of utility methods to do String Match Count

Description

The list of methods to do String Match Count are organized into topic(s).

Method

intcountMatches(String str, String match)
Counts the occurence of a String in another String
int counter = 0;
while (str.contains(match)) {
    counter++;
    str = str.substring(str.indexOf(match) + match.length());
return counter;
intcountMatches(String str, String sub)
count Matches
if (!hasText(str) || !hasText(sub)) {
    return 0;
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
    count++;
    idx += sub.length();
...
intcountMatches(String str, String sub)
Returns the number of times sub occurs in str
int count = 0;
int initpos = 0;
while (str.indexOf(sub, initpos) != -1) {
    count = count + 1;
    initpos = str.indexOf(sub, initpos) + 1;
return count;
intcountMatches(String str, String sub)

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

A null or empty ("") String input returns 0.

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", null)  = 0 StringUtils.countMatches("abba", "")    = 0 StringUtils.countMatches("abba", "a")   = 2 StringUtils.countMatches("abba", "ab")  = 1 StringUtils.countMatches("abba", "xxx") = 0 
if (isEmpty(str) || isEmpty(sub)) {
    return 0;
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
    count++;
    idx += sub.length();
...
intcountMatches(String str, String sub)
count Matches
if (str == null || str.length() == 0 || sub == null || sub.length() == 0) {
    return 0;
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
    count++;
    idx += sub.length();
...
intcountMatches(String str, String sub)
count Matches
if ((isEmpty(str)) || (isEmpty(sub))) {
    return 0;
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
    count++;
    idx += sub.length();
...
intcountMatches(String str, String sub)
count Matches
if (isEmpty(str) || isEmpty(sub)) {
    return 0;
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
    count++;
    idx += sub.length();
...
intcountMatches(String str, String sub)

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

A null or empty ("") String input returns 0.

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", null)  = 0 StringUtils.countMatches("abba", "")    = 0 StringUtils.countMatches("abba", "a")   = 2 StringUtils.countMatches("abba", "ab")  = 1 StringUtils.countMatches("abba", "xxx") = 0 
if (isEmpty(str) || isEmpty(sub)) {
    return 0;
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
    count++;
    idx += sub.length();
...
intcountMatches(String string, char find)
count Matches
int count = 0;
for (int i = 0; i < string.length(); i++) {
    if (string.charAt(i) == find)
        count++;
return count;
intcountMatches(String string, char toMatch)
Count the number of times a specific character is present in a string
int occurrences = 0;
for (char c : string.toCharArray()) {
    if (c == toMatch) {
        occurrences++;
return occurrences;