Member-only story
Split a String into an array in Swift?
1 min readNov 1, 2019
A new function split
in Swift 4
import Foundation
let sayHello = "Hello Swift ! 2019";
let result = sayHello.split(separator: " ")
print(result)
Output:
["Hello", "Swift", "!", "2019"]
Accessing values:
print(result[0]) // Hello
print(result[1]) // Swift
print(result[2]) // !
print(result[3]) // 2019
Xcode 8.1 / Swift 3.0.1
Here is the way multiple delimiters with array.
import Foundation
let mathString: String = "12-37*2/5"
let numbers = mathString.components(separatedBy: ["-", "*", "/"])
print(numbers)
Output:
["12", "37", "2", "5"]
For more about iOS development : https://medium.com/@javedmultani16
Some useful codes and libraries: https://github.com/javedmultani16
Know more about me: https://www.linkedin.com/in/javedmultani16/