Count number of letters in string

This example will count the occurrences of a character in string using java, java 8, guava, apache commons and spring framework. Each snippet will use the phrase "she saw a fish on the seashore and I'm sure The fish she saw on the seashore was a saw-fish." and count the number of 'a' that exist. In a comparable illustration we demonstrate how count the number of characters in string using groovy.

Straight up Java

Using a straight up java technique we count the number of letters in specified string by iterating over each string index and checking if it equals the letter 'a'.

@Test
public void number_of_cccurrences_of_char_in_string_java() {

    String stringToSearch = "she saw a fish on the seashore and "
            + "I'm sure The fish she saw on the seashore was a saw-fish.";
    
    String letter = "s";

    int i = 0, count = 0;
    while ((i = stringToSearch.indexOf(letter, i++)) != -1) {
        count++;
        i += letter.length();
    }

    assertEquals(14, count);
}

Java 8

Using java 8 we will count all the letters in the String first converting the string to a stream by calling String.chars(). Next we will filter the stream by passing a lambda expression that will evaluate to true if the character value equals 's'. Finally by calling a stream terminal operation we will count the total number of 's' contained in the string.

@Test
public void number_of_occcurrences_of_char_in_string_java8() {

    String stringToSearch = "she saw a fish on the seashore and "
            + "I'm sure The fish she saw on the seashore was a saw-fish.";

    long count = stringToSearch.toLowerCase().chars().filter(e -> e == 's')
            .count();

    assertEquals(14, count);
}

Google Guava

This snippet shows one way to count the number of times a letter occurs in a string with guava. There is an open issue to create a new method Strings.count(String string, String substring). Using guava Splitter we will split the string on the letter 'a' which will return an Iterable of elements. Then to find the count we will call Iterable.size which return 15.

@Test
public void number_of_cccurrences_of_char_in_string_guava () {
    
    String stringToSearch = "she saw a fish on the seashore and "
            + "I'm sure The fish she saw on the seashore was a saw-fish.";
    
    String letter = "s";

    int count = Iterables.size(Splitter.on(letter).split(stringToSearch)) - 1;
    
    assertEquals(14, count);
}

Apache Commons

This snippet will counts how many times a string appears in another string with apache commons by using StringUtils.countMatches.

@Test
public void number_of_cccurrences_of_char_in_string_apache_commons () {

    String stringToSearch = "she saw a fish on the seashore and "
            + "I'm sure The fish she saw on the seashore was a saw-fish.";
    
    String letter = "s";

    int count = StringUtils.countMatches(stringToSearch, letter);
    assertEquals(14, count);
}

Spring Framework

Springframework StringUtils.countOccurrencesOf will also count the occurrences of the substring in stringToSearch.

@Test
public void number_of_cccurrences_of_char_in_string_springframework () {
    
    String stringToSearch = "she saw a fish on the seashore and "
            + "I'm sure The fish she saw on the seashore was a saw-fish.";
    
    String letter = "s";

    int count = org.springframework.util.
            StringUtils.countOccurrencesOf(stringToSearch, letter);

    assertEquals(14, count);
}