Kotlin program to demonstrate the example of split() function
// Example of split() function
fun main()
{
// Demonstrating split() function
// separate words from white-spaces
val pattern1 = Regex("\\s+")
val res1 : List<String> = pattern1.split("Hello, world! How are you?")
// Prints the words using forEach loop
res1.forEach { term -> println(term) }
println()
// separate words from commas
val pattern2 = Regex(",")
val res2 : List<String> = pattern2.split("Hello,Hi,There")
// Prints the words using forEach loop
res2.forEach { term -> println(term) }
println()
}
Kotlin program to demonstrate the example of split() function
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer