在Objective-C中,我们可以调用componentsJoinedByString生成一个字符串,数组的每个元素都由提供的字符串分隔。虽然Swift在String上有一个componentsSeparatedByString方法,但是在Array上并没有出现这种情况:
'Array<String>' does not have a member named 'componentsJoinedByString'
Swift中componentsSeparatedByString的逆是什么?
Swift 3.0:
类似于Swift 2.0,但是API重命名已经将joinWithSeparator重命名为join(separator :)。
let joinedString = ["1","2","3","4","5"].joined(separator: ",") // joinedString: String = "1,2,3,4,5"
有关详细信息,请参阅Sequence.join(separator:)。
Swift 2.0:
您可以使用SequenceType上的joinWithSeparator方法将字符串数组与字符串分隔符联接。
let joinedString = ["1","5"].joinWithSeparator(",5"
有关详细信息,请参阅SequenceType.joinWithSeparator(_:)。
Swift 1.0:
您可以使用String上的连接标准库函数将字符串数组与字符串连接。
let joinedString = ",".join(["1","5"]) // joinedString: String = "1,5"
或者如果你愿意,你可以使用全局标准库函数:
let joinedString = join(",",["1",5"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。