【问题标题】:How to split a [String] into rows?如何将 [String] 拆分为行?
【发布时间】:2015-06-02 06:56:33
【问题描述】:

我正在使用UITextView 将一些值添加到数组中。

如果它们之间有换行符 (\n) 或逗号 (,),我想将文本与 UITextView 分隔成单个项目。

var values = self.textLabel.text.componentsSeparatedByString("\n")
for item in values {
    if item != "" {
        cellDataSet.insert([item, false], atIndex: 0)
    }
}

【问题讨论】:

    标签: ios arrays string swift


    【解决方案1】:

    如果您想在多个tokens 上分隔一个String,请使用componentsSeparatedByCharactersInSet(_:)

    例子:

    let text = "This is, some, text; With multiple | seperators"
    let separators = NSCharacterSet(charactersInString: ",;|")
    let values = text.componentsSeparatedByCharactersInSet(separators)
    

    【讨论】:

    • 谢谢,这很容易。
    【解决方案2】:

    您可以使用全局可用的split 函数来做同样的事情。

    let stringToSplit = "Words,Separated\nBy,Comma,Or\nNewline"
    let outputArray = split(stringToSplit) {$0 == "," || $0 == "\n"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 2020-04-16
      • 2019-09-19
      • 2015-10-27
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多