How do I split the string [String] into lines?
I am using UITextView
to add some values ββto an array.
I would like to highlight text from UITextView
in separate elements if they have a newline ( \n
) or comma ( ,
) between them.
var values = self.textLabel.text.componentsSeparatedByString("\n")
for item in values {
if item != "" {
cellDataSet.insert([item, false], atIndex: 0)
}
}
+3
source to share
2 answers
If you want to split String
into multiple tokens usecomponentsSeparatedByCharactersInSet(_:)
Example:
let text = "This is, some, text; With multiple | seperators"
let separators = NSCharacterSet(charactersInString: ",;|")
let values = text.componentsSeparatedByCharactersInSet(separators)
+5
source to share