In Swift, you can split a string by new line using the split
method with the newline delimiter "\n"
.
In this example,
str
with a value containing new lines.split
method with the newline delimiter "\n"
to split the string into an array of substrings.let str = "Hello\nWorld\n"
let substrings = str.split(separator: "\n")
for sub in substrings {
print(sub)
}
Hello World
In this tutorial, we learned How to Split a String by New Line in Swift language with well detailed examples.