1String someString = "elephant";
2long count = someString.chars().filter(ch -> ch == 'e').count();
3assertEquals(2, count);
4
5long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
6assertEquals(2, count2);
7
1public class StringNumberOfOccurenceLetter {
2
3 private static int countOccurences(String word, char character){
4
5 int count = 0;
6 for (int i = 0; i < word.length() ; i++) {
7 if (word.charAt(i)==character){
8 count++;
9 }
10 }
11 return count;
12
13 }
14 public static void main(String[] args) {
15
16
17 int count = countOccurences("dddssad", 'a');
18 }
19
20
21}
1String line = "apples.oranges.bananas";
2int count = line.length() - line.replace(".", "").length();
3System.out.println(count);//displays 2