In Kotlin, you can split a string by new line using the split
function with the regex "\\n"
.
In this example,
str
with a value containing new lines.split
function with the regex "\\n"
to split the string into an array of substrings.fun main() {
val str = "Hello\nWorld\n"
val substrings = str.split(Regex("\\n"))
for (sub in substrings) {
println(sub)
}
}
Hello World
In this tutorial, we learned How to Split a String by New Line in Kotlin language with well detailed examples.