ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.