I'm using a UITextView
to add some values into an array.
我正在使用UITextView将一些值添加到数组中。
I'd like to separate the text from the UITextView
into single items if they have a newline (\n
) or a comma (,
) between them.
我想将文本从UITextView分成单个项目,如果它们之间有换行符(\ n)或逗号(,)。
var values = self.textLabel.text.componentsSeparatedByString("\n")
for item in values {
if item != "" {
cellDataSet.insert([item, false], atIndex: 0)
}
}
2 个解决方案
#1
5
If you want to separate a String
on multiple tokens, use componentsSeparatedByCharactersInSet(_:)
如果要在多个标记上分隔String,请使用componentsSeparatedByCharactersInSet(_ :)
Example:
例:
let text = "This is, some, text; With multiple | seperators"
let separators = NSCharacterSet(charactersInString: ",;|")
let values = text.componentsSeparatedByCharactersInSet(separators)
#2
5
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"}
#1
5
If you want to separate a String
on multiple tokens, use componentsSeparatedByCharactersInSet(_:)
如果要在多个标记上分隔String,请使用componentsSeparatedByCharactersInSet(_ :)
Example:
例:
let text = "This is, some, text; With multiple | seperators"
let separators = NSCharacterSet(charactersInString: ",;|")
let values = text.componentsSeparatedByCharactersInSet(separators)
#2
5
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"}