In Swift, you can split a string using the split
method.
In this example,
str
with a value of "Hello World"
.split
method with a space as the delimiter.let str = "Hello World"
let parts = str.split(separator: " ")
for part in parts {
print(part)
}
Hello World
In this example,
str
with a value of "apple,banana,cherry"
.split
method with a comma as the delimiter.let str = "apple,banana,cherry"
let parts = str.split(separator: ",")
for part in parts {
print(part)
}
apple banana cherry
In this tutorial, we learned How to Split a String in Swift language with well detailed examples.