This article explores different ways to check whether a string contains a substring in Kotlin.

1. Using contains() function

The standard solution to check if the string contains the specified sequence of characters as a substring is with the contains() function. Here’s an example of its usage:

Download Code

 
If you need to perform the comparison with the case ignored, you can pass the boolean value true as a second parameter to the contains() function.

Download Code

2. Using In operator

The Kotlin way to check if a string contains a substring is with the In operator, which provides shorter and more readable syntax. The expression a in b is translated to b.contains(a) and the expression a !in b is translated to !b.contains(a). In other words, In is equivalent to calling the contains() function.

Download Code

That’s all about determining whether a string contains a substring in Kotlin.