countOccurrences static method
Count occurrences of substring
text - The main text
substring - The substring to count
Returns number of occurrences
Implementation
static int countOccurrences(String text, String substring) {
if (substring.isEmpty) return 0;
return text.split(substring).length - 1;
}