1. Overview

One of the most common tasks in software development is to check substrings within a string. For example, we may want to search some patterns or replace sentences in large text files.

In this tutorial, we’ll see some efficient methods to check if a string contains a substring in Kotlin.

2. Implementation

There are built-in functions in Kotlin to check substrings and some valuable operations to simulate the expected functionality. Let’s start with the native Kotlin support.

2.1. Native contains Method

First, using core Kotlin support, we can use the contains method that returns true or false depending on if the substring is contained in the string object that is calling:

@Test
fun `given a string when search for contained substrings then should return true`() {
    val string = "Kotlin is a programming language"
    val substring = "programming"

    val contains = string.contains(substring)
    assertThat(contains).isTrue
}

Alternatively, we can obtain the same result by using the in operator to search for substrings:

@Test
fun `given a string when search for substrings in the string then should return true`() {
    val string = "Kotlin is a programming language"
    val substring = "programming"

    val contains = substring in string
    assertThat(contains).isTrue
}

We might want to search for substrings ignoring the character case for some scenarios. For that purpose, the contains method can receive a flag ignoreCase to indicate that we want to search substrings in that way:

@Test
fun givenString_whenSearchSubstringsIgnoringCase_thenReturnsTrue() {
    val string = "Kotlin is a programming language"
    val substring = "Programming Language"

    val contains = string.contains(substring, true)
    assertThat(contains).isTrue
}

2.2. Regular Expressions

Another way to check substrings is by defining regular expressions to search for matching patterns within the string:

@Test
fun givenString_whenSearchSubstringsUsingRegex_thenReturnsTrue() {
    val string = "Kotlin is a programming language"
    val substring = "programming language"
    val regex = substring.toRegex();

    val contains = regex.containsMatchIn(string)
    assertThat(contains).isTrue
}

2.3. Index of Substrings

Kotlin supports searching and retrieving the index of a given substring. The method lastIndexOf() will return a non-negative number if the substring is found within the main string:

@Test
fun givenString_whenSearchSubstringsByIndexOf_thenReturnsNonNegative() {
    val string = "Kotlin is a programming language"
    val substring = "programming"
    val index = string.lastIndexOf(substring);

    assertThat(index).isNotNegative
}

2.4. Finding Substrings

With the method findLastAnyOf(), we can find the occurrences of a given substring. The method will return a Pair object filled with the index of the last appearance of the substrings and the given substring or null if none of the substrings matches:

@Test
fun givenString_whenSearchSubstringsWithFindLast_thenReturnsNotNullPair() {
    val string = "Kotlin is a programming language"
    val substring = "programming language"
    val found = string.findLastAnyOf(listOf(substring));

    assertThat(found).isNotNull
}

3. Conclusion

In this tutorial, we’ve explored some efficient techniques to check substrings within other strings. There’s native support that Kotlin exposes and some handy methods to achieve this goal.

As always, the complete code for this article is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.