java check how many specific letter in word code example
Example 1: how to test how many of one character is in a string java
int count = string.length() - string.replaceAll("g","").length()
Example 2: check how many of a word is in a string
function countOccurences(str,word)
{
// split the string by spaces in a
String a[] = str.split(",");
// search for pattern in a
int count = 0;
for (int i = 0; i < a.length; i++)
{
// if match found increase count
if (word.equals(a[i]))
count++;
}
return count;
}