countOccurrencesOf static method

int countOccurrencesOf(
  1. String str,
  2. String sub
)

Count occurrences of substring in string

Implementation

static int countOccurrencesOf(String str, String sub) {
  if (!hasLength(str) || !hasLength(sub)) return 0;
  int count = 0;
  int pos = 0;
  while ((pos = str.indexOf(sub, pos)) != -1) {
    count++;
    pos += sub.length;
  }
  return count;
}