Example of find() function in Kotlin

Learn about the find() function in Kotlin, and demonstrate the example of the find() function.
Submitted by IncludeHelp, on March 25, 2022

find() Function

The find() function returns the first match of a regular expression in the given input, beginning at the specified startIndex.

Syntax:

fun find(
    input: CharSequence,
    startIndex: Int = 0
): MatchResult?

Kotlin program to demonstrate the example of find() function

// Example of find() function
fun main()
{
	// Regex to match "oo" in a string
	val pattern = Regex("oo")
	val res : MatchResult? = pattern.find("noncooperations coordination", 2)
	println(res ?.value)
    
	val res1 : MatchResult? = pattern.find("noncooperations coordination", 20)
	println(res1 ?.value)
}

Output:

oo
null

Explanation:

In the first statement "oo" is there from the index 2, but in the second statement "oo" is not there from the index 20.

Kotlin Regular Expression (Regex) Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.