Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Introduction

The String is a basic data type in a programming language. Therefore, it’s important to know the basic operations and typical use cases for a String. In this tutorial, we’ll see various methods of counting occurrences of a specific character in a String.

2. Using count()

A String is a sequence of characters. Thanks to that, we can use the string extension function to count occurrences of a single character. The count() function returns the number of characters matching the given predicate:

val string = "hello world, baeldung"

assertEquals(2, string.count { it == 'e' })

3. Using Iteration

The easiest way to obtain occurrences of character is to iterate over the characters in the string using a for-loop. Then, we can increment the counter if the current character matches the specified character:

val string = "hello world, baeldung"
var counter = 0
for (c in string) {
    if (c == 'e') {
        counter++
    }
}

assertEquals(2, counter)

4. Using replace()

Another way to count occurrences is using the replace() extension function. Firstly, we can remove all occurrences of the specified character from the string using the replace() function. As a result, it returns a new string without the character we replaced. Further, we can calculate the difference between the new string length and that of the original string. The result will be the number of occurrences of the specified character. Let’s see an example:

val string = "hello world, baeldung"
val count = string.length - string.replace("e", "").length

assertEquals(2, count)

5. Using Regular Expressions

Another method to count occurrences is to use regular expressions. We can compile our character as a regular expression, and then, we create a matcher and count the matched occurrences:

val string = "hello world, baeldung"
val matcher = Pattern.compile("e").matcher(string)
var counter = 0

while (matcher.find()) {
    counter++
}

assertEquals(2, counter)

6. Using the Map Collection

Let’s try to count occurrences using map collection. Firstly, we can store the count of each distinct character present in the string by iterating over it. The keys in the map are characters from the string, so we can easily access occurrences values by a specific character:

val string = "hello world, baeldung"
val occurrencesMap = mutableMapOf<Char, Int>()
for (c in string) {
    occurrencesMap.putIfAbsent(c, 0)
    occurrencesMap[c] = occurrencesMap[c]!! + 1
}

assertEquals(2, occurrencesMap['e'])

7. Conclusion

In this article, we’ve seen the most common ways to count specific character occurrences in a string in Kotlin. Kotlin provides some useful built-in functions that allow our code to be cleaner and follow the conventions of the language. We also saw some more complex solutions to this problem. As usual, all the examples are available over on GitHub.

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!