美文网首页
Swift数组和字符串相互转换(更新至Swift 4.0)

Swift数组和字符串相互转换(更新至Swift 4.0)

作者: 冷大大_hawkleng | 来源:发表于2016-05-29 15:23 被阅读2415次

Swift 4.0

数组转字符串

let array = ["a", "b", "c"]
let s = array.joined(separator: ",")
print(s)
// 结果为:a,b,c

Swift 2.0

字符串转数组

let str = "1,2,3"
let array = str.componentsSeparatedByString(",")

数组转字符串

let array = ["1", "2", "3"]
let str = ",".join(array)
// or
let str = array.joinWithSeparator(",")
Swift Dev for MacOS

相关文章

网友评论

      本文标题:Swift数组和字符串相互转换(更新至Swift 4.0)

      本文链接:https://www.haomeiwen.com/subject/ivdqdttx.html