This article explores different ways to check if a string contains any of the given substrings in Kotlin.

The idea is to call the contains() function to match against each substring. There are several ways to do this, which are covered below in detail:

1. Using Loop

The following solution creates a utility function that returns true on the first match of the substring from the specified list, using a loop.

Download Code

 
To get the actual substring contained in the string, do like:

Download Code

2. Using any() function

The idea here is to call the any() function on the list, which returns true if any item matches the provided predicate. To check if a string contains a substring, use the contains() function as the predicate.

Download Code

 
To get the actual substring contained in the string, use the first() function with the contains() function:

Download Code

That’s all about checking if a string contains any of the given substrings in Kotlin.