Kotlin String findAnyOf()

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

ParameterDescription
stringsA collection of substrings to find in the string.
startIndexThe index to start searching from. The default is 0.
ignoreCaseSpecifies whether the search is case-insensitive. The default is false.
Parameters of String.findAnyOf() function

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.

  1. Take a string value in text.
  2. Define a collection of countries, e.g., countries.
  3. Call findAnyOf() function on text with the countries collection as the argument. The function returns a Pair containing the index of the first occurrence and the country found, or null if none of the specified countries are present in the string.
  4. 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.

  1. Take a string value in text.
  2. Define a collection of keywords, e.g., keywords.
  3. Call findAnyOf() function on text with the keywords collection as the argument. The function returns a Pair containing the index of the first occurrence and the keyword found, or null if none of the specified keywords are present in the string.
  4. 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.