Home »
Kotlin »
Kotlin Programs
Example of findAll() function in Kotlin
Learn about the findAll() function in Kotlin, and demonstrate the example of the findAll() function.
Submitted by IncludeHelp, on March 25, 2022
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>
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.
Kotlin Regular Expression (Regex) Programs »