Q:

Example of findAll() function in Kotlin

0

findAll() Function

The findAll() function returns a sequence of all occurrences of a regular expression within the input string, beginning at the specified startIndex.

Syntax:

fun findAll(
    input: CharSequence,
    startIndex: Int = 0
): Sequence<MatchResult>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Kotlin program to demonstrate the example of findAll() function

// Example of findAll() function
fun main()
{
	// Regex to match a 5 letters pattern 
	// beginning with He
	val pattern = Regex("He...")
	val res1 : Sequence<MatchResult> = pattern.findAll("Hello, World!", 0)

	// Prints all the matches using forEach loop
	res1.forEach()
	{
		matchResult -> println(matchResult.value)
	}
	println()

	val res2 : Sequence<MatchResult> = pattern.findAll("Hello, I'm saying Hello Boss!", 0)

	// Prints all the matches using forEach loop
	res2.forEach()
	{
		matchResult -> println(matchResult.value)
	}
	println()    
}

Output:

Hello

Hello
Hello

Explanation:

In the first statement "Hello" is there one time, and in the second statement "Hello" is there two times.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Example of matches() function in Kotlin... >>
<< Example of find() function in Kotlin...