Kotlin String findAnyOf() Tutorial
The String.findAnyOf()
function in Kotlin is used to find the first occurrence of any substring in a collection of substrings within the original string. It returns a Pair
containing the index of the first occurrence and the substring found, or null
if none of the specified substrings are present in the string.
In this tutorial, we’ll explore the syntax of the findAnyOf()
function and provide examples of its usage in Kotlin strings.
Syntax
The syntax of the findAnyOf()
function is as follows:
fun CharSequence?.findAnyOf(
strings: Collection<String>,
startIndex: Int = 0,
ignoreCase: Boolean = false
): Pair<Int, String>?
where
Parameter | Description |
---|---|
strings | A collection of substrings to find in the string. |
startIndex | The index to start searching from. The default is 0 . |
ignoreCase | Specifies whether the search is case-insensitive. The default is false . |
The findAnyOf()
function is an extension function available for instances of CharSequence
, which includes String
. It searches for the first occurrence of any specified substring in the strings
collection and returns a Pair
containing the index of the first occurrence and the substring found.
Examples for String findAnyOf() function
1. Find the Index of Any Country in the given String
In this example, we’ll use findAnyOf()
to find the index of the first occurrence of any specified country in a given string.
- Take a string value in
text
. - Define a collection of countries, e.g.,
countries
. - Call
findAnyOf()
function ontext
with thecountries
collection as the argument. The function returns aPair
containing the index of the first occurrence and the country found, ornull
if none of the specified countries are present in the string. - You may print the resulting
Pair
to the console output.
Kotlin Program
fun main() {
val text = "Here are the countries: USA, Canada, Mexico"
// Define a collection of countries
val countries = listOf("USA", "Canada", "Mexico")
// Using findAnyOf() to find the index of the first occurrence of any specified country in the string
val result = text.findAnyOf(countries)
// Printing the original string and the resulting Pair
println("Original String:\n$text\n")
println("Index and Country: $result")
}
Output
Original String:
Here are the countries: USA, Canada, Mexico
Index and Country: (24, USA)
2. Find the Index of Any Keyword in the given String
In this example, we’ll use findAnyOf()
to find the index of the first occurrence of any specified keyword in a given string.
- Take a string value in
text
. - Define a collection of keywords, e.g.,
keywords
. - Call
findAnyOf()
function ontext
with thekeywords
collection as the argument. The function returns aPair
containing the index of the first occurrence and the keyword found, ornull
if none of the specified keywords are present in the string. - You may print the resulting
Pair
to the console output.
Kotlin Program
fun main() {
val text = "In recent times Python, Kotlin, and Java are leading the development."
// Define a collection of keywords
val keywords = listOf("Kotlin", "Java", "Python")
// Using findAnyOf() to find the index of the first occurrence of any specified keyword in the string
val result = text.findAnyOf(keywords)
// Printing the original string and the resulting Pair
println("Original String:\n$text\n")
println("Index and Keyword: $result")
}
Output
Original String:
In recent times Python, Kotlin, and Java are leading the development.
Index and Keyword: (16, Python)
Summary
In this tutorial, we’ve covered the findAnyOf()
function in Kotlin strings, its syntax, and how to use it to find the index of the first occurrence of any substring in a collection of substrings within the original string. This function is useful for extracting specific information based on a set of specified substrings.