java – How to count the number of occurrences of a character in a string?

Question:

There is a need to implement a check for multiple commas in a string. Those. only one comma can be used.

But in general, it is enough to know the number of occurrences of a character in a string. The rest will not be a problem to add.

What are the ways to do this (count the number of occurrences of a character)?

Answer:

  1. Регулярка
  2. Использовать StringUtils.countMatches из org.apache.commons.lang3.StringUtils

    int occurrence = StringUtils.countMatches("a,b,c,d", ",");
    System.out.println(occurrence);
    
  3. Использовать string.split

    String string = "a,b,c,d";
    String []splitArray = string.split("\\,");
    System.out.println("Запятых тут : " + (splitArray.length - 1) + " штук.");
    
  4. String testString =  "a,b,c,d";
    int occurrencesCount = testString.length() - testString.replace(",", "").length();
    System.out.println(occurrencesCount);
    
  5. Java8

    String testString =  "a,b,c,d";
    long occurrencesCount = testString.chars().filter(ch -> ch == ',').count();
    System.out.println(occurrencesCount);
    
  6. Еще какой-нибудь способ


UPD found a similar thread on enSO: How do I count the number of occurrences of a char in a String? , a few examples from there:

  • Spring Framework

     int spring = org.springframework.util.StringUtils.countOccurrencesOf(testString, ","); System.out.println("spring = " + spring);
  • replaceAll

     int replaceAll = testString.replaceAll("[^,]", "").length(); System.out.println("replaceAll = " + replaceAll);
  • Java8

     long java8 = testString.codePoints().filter(ch -> ch == ',').count(); System.out.println("java8 = " + java8);
  • StringTokenizer

     int stringTokenizer = new StringTokenizer(" " +testString + " ", ",").countTokens()-1; System.out.println("stringTokenizer = " + stringTokenizer);

    But in this case, you need to be careful, because, for example, for the string abcd this example will work, but for the string a … bc … d or … abcd or a …. b ….. .c ….. d … etc. – will not be. As a result, the sequence of dots between each letter is counted as only one character.

Scroll to Top