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}
1public static int count(String str, String target) {
2 return (str.length() - str.replace(target, "").length()) / target.length();
3}
4
1public class CountStringOccurence {
2public static void main(String[] args) {
3
4int count= countOccurences("aaassssddadad",'s');
5System.out.println(count);
6
7}
8
9private static int countOccurences(String word, char character){
10
11 int count = 0;
12 for(int i = 0; i < word.length(); i++){
13
14 if(word.chartAt(i) == character){
15
16 count++; }
17 }
18 return count;
19}
20}
21
22
1String line = "apples.oranges.bananas";
2int count = line.length() - line.replace(".", "").length();
3System.out.println(count);//displays 2