草庐IT

separatedBy

全部标签

swift - 组件(separatedBy :) versus . 拆分(分隔符:)

在Swift4中,Apple在String结构中引入了新方法.split(separator:)。所以用空格分割一个字符串会更快,例如.letstr="MynameisSudhir"str.components(separatedBy:"")//orstr.split(separator:"") 最佳答案 撇开性能不谈,split(separator:)和components(separatedBy:)在处理空子序列的方式上存在重要区别。如果您的输入包含尾随空格,它们将产生不同的结果:letstr="MynameisSudhir"/

ios - 字符串在 separatedBy 之后只返回数字

我正在尝试像下面这样分隔字符串:letpath="/Users/user/Downloads/history.csv"do{letcontents=tryNSString(contentsOfFile:path,encoding:String.Encoding.utf8.rawValue)letrows=contents.components(separatedBy:"\n")print("contents:\(contents)")print("rows:\(rows)")}catch{}我有两个文件,它们看起来几乎相同。第一个文件的输出是这样的:输出文件1:contents:201