好吧,所以说我有一个类似的字符串 let myString = "my string: hello" 我想用“,”代替“:”.我得到了这个: let characters = map(Array(myString), {$0 == ":" ? "," : $0}) 返回MapCollectionView Array Character,Cha
let myString = "my string: hello"
我想用“,”代替“:”.我得到了这个:
let characters = map(Array(myString), {$0 == ":" ? "," : $0})
返回MapCollectionView< Array< Character>,Character>.有没有一种简单的方法将其转换回String?
那这个呢?通过“减少”它们将所有字符组合成一个字符串操作符:
let str = Array(characters).reduce("", combine: +) println(str) // Output: my string, hello
更新:替代(可能更好)的解决方案:
var str = "" str.extend(characters)
使用extend(),可以在没有中间数组的情况下完成字符串替换:
let myString = "my string: hello" as String var myNewString = "" myNewString.extend(map(myString.generate(), {$0 == ":" ? "," : $0} ))