Java count character occurrences in a string:
/**
* @author lautturi.com
* Java example: Check how many times a character appears in a string
*/
import java.util.*;
import java.io.File;
public class Lautturi {
public static void main(String[] args) {
String str = "hello lautturi java world!";
int count = (int) str.chars().filter(num -> num == 'a').count();
System.out.println("Times:"+count);
}
}Source:wwal.wutturi.comoutput:
The character a appears 3 times
Times:3