To split a string in Kotlin, you can use the split
function.
In this example,
str
with a value of "Hello World"
.split
function with a space as the delimiter.fun main() {
val str = "Hello World"
val parts = str.split(" ")
println(parts)
}
[Hello, World]
In this example,
str
with a value of "apple,banana,cherry"
.split
function with a comma as the delimiter.fun main() {
val str = "apple,banana,cherry"
val parts = str.split(",")
println(parts)
}
[apple, banana, cherry]
In this tutorial, we learned How to Split a String in Kotlin language with well detailed examples.