Kotlin – Count occurrences of a substring in a string

Count occurrences of a substring in a string in Kotlin

To count the number of occurrences of a substring (search string) in a string (content string) in Kotlin, you can use While loop to iterate over the length of the content string and check for the occurrences of the search string.

We can find overlapping or non-overlapping occurrences, by modifying the While loop just a little, which we shall see in the following examples.

Let us take a scenario where content string is "ababababa" and search string is "aba".

Counting non-overlapping occurrences

ababababa   <- given string
aba         <- first occurrence
    aba     <- second occurrence

Counting overlapping occurrences

ababababa   <- given string
aba         <- first occurrence
  aba       <- second occurrence
    aba     <- third occurrence
      aba   <- fourth occurrence

Examples

Count non-overlapping occurrences of substring in a string

In the following program, we count the non-overlapping occurrences of a search string in a content string.

Kotlin Program

fun countOccurrences(str: String, searchStr: String): Int {
    var count = 0
    var startIndex = 0

    while (startIndex < str.length) {
        val index = str.indexOf(searchStr, startIndex)
        if (index >= 0) {
            count++
            startIndex = index + searchStr.length
        } else {
            break
        }
    }

    return count
}

fun main() {
    val contentString = "ababababa"
    val searchString = "aba"

    val occurrenceCount = countOccurrences(contentString, searchString)
    println(occurrenceCount)
}

Output

2

Explanation

ababababa   <- given string
aba         <- first occurrence
    aba     <- second occurrence

Count overlapping occurrences of substring in a string

In the following program, we count the overlapping occurrences of a search string in a content string.

Kotlin Program

fun countOccurrences(str: String, searchStr: String): Int {
    var count = 0
    var startIndex = 0

    while (startIndex < str.length) {
        val index = str.indexOf(searchStr, startIndex)
        if (index >= 0) {
            count++
            startIndex = index + 1
        } else {
            break
        }
    }

    return count
}

fun main() {
    val contentString = "ababababa"
    val searchString = "aba"

    val occurrenceCount = countOccurrences(contentString, searchString)
    println(occurrenceCount)
}

Output

4

Explanation

ababababa   <- given string
aba         <- first occurrence
  aba       <- second occurrence
    aba     <- third occurrence
      aba   <- fourth occurrence

The only difference between counting the overlapping and non-overlapping occurrences, with respect to the program, is that how we update the startIndex value. For counting non-overlapping occurrences, we update the startIndex with index of the search string + length of the substring. Whereas for counting overlapping occurrences, we update the startIndex with index + 1.