This article explores different ways to check whether a String contains a character in Kotlin.

1. Using indexOf() function

The indexOf() function returns the index of the first occurrence of a char within the string. If the character does not exist, it returns -1. This value can be used to check if the given character appears in the string or not, as shown below:

Download Code

2. Using contains() function

Alternatively, we can use the contains() function to check if a char is present in the string or not.

Download Code

 
We can make the comparison case-insensitive, as follows:

Download Code

3. Using in operator

The most idiomatic way to check if a string contains a character is using the in operator. This is equivalent to calling the contains() function, but offers shorter and more readable syntax.

Download Code

That’s all about checking whether a String contains a character in Kotlin.