count how many times a certain character appears in string java code example
Example 1: count occurrences of character in string java 8
String someString = "elephant";
long count = someString.chars().filter(ch -> ch == 'e').count();
assertEquals(2, count);
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
assertEquals(2, count2);
Example 2: check how many times a character appears in a string java
str.chars().filter(num -> num == '$').count()