我必须使用stringByReplacingOccurrencesOfString
来处理多种情况,我想知道是否有更好的方式将下面的代码写入可能的单行?将多个stringByReplacingOccurrencesOfString合并为一个
let newString = list.stringByReplacingOccurrencesOfString("\n", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
let newString2 = newString.stringByReplacingOccurrencesOfString("(", withString: "")
let newString3 = newString2.stringByReplacingOccurrencesOfString(")", withString: "")
let newString4 = newString3.stringByReplacingOccurrencesOfString(" ", withString: "")
我的第一个问题是:*“为什么需要?”* - 为什么需要删除括号,空格和换行符从字符串?也许这个问题可以在更早的时候得到解决,在哪里创建字符串? –
@MartinR:这通常是对不使用RegEx的尝试。 –
@VatsalManot:这可能是,我不知道,这就是为什么我问。也可能是这个字符串是通过在NSArray或类似的东西上调用'description'来获得的。 –