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
Victor
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
fguchelaar
source
to share
You can use a globally available function split
to do the same.
let stringToSplit = "Words,Separated\nBy,Comma,Or\nNewline"
let outputArray = split(stringToSplit) {$0 == "," || $0 == "\n"}
+5
Ankit goel
source
to share