Reputation:
I'm using a UITextView
to add some values into an array.
I'd like to separate the text from the UITextView
into single items if they have a newline (\n
) or a comma (,
) between them.
var values = self.textLabel.text.componentsSeparatedByString("\n")
for item in values {
if item != "" {
cellDataSet.insert([item, false], atIndex: 0)
}
}
Upvotes: 3
Views: 139
Reputation: 4899
If you want to separate a String
on multiple tokens, use componentsSeparatedByCharactersInSet(_:)
Example:
let text = "This is, some, text; With multiple | seperators"
let separators = NSCharacterSet(charactersInString: ",;|")
let values = text.componentsSeparatedByCharactersInSet(separators)
Upvotes: 4
Reputation: 6495
You can use the globally available split
function to do the same.
let stringToSplit = "Words,Separated\nBy,Comma,Or\nNewline"
let outputArray = split(stringToSplit) {$0 == "," || $0 == "\n"}
Upvotes: 4