Swift 分割字符串

前端之家收集整理的这篇文章主要介绍了Swift 分割字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

直接上代码

Swift 1

var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil

Swift 2

Swift 2: Xcode 7 beta 1

let fullName = "First Last"
let fullNameArr = split(fullName.characters){$0 == " "}.map{String($0)}

fullNameArr[0] // First
fullNameArr[1] // Last

Swift 2: Xcode 7 beta 2

let fullName = "First Last"
let fullNameArr = split(fullName.characters){$0 == " "}.map(String.init)

fullNameArr[0] // First
fullNameArr[1] // Last

Swift 2: Xcode 7 beta 5

let fullName = "First Last"
let fullNameArr = fullName.split {$0 == " "}

fullNameArr[0] // First
fullNameArr[1] // Last

Swift 2: Xcode 7

let fullName = "First Last"
let fullNameArr = fullName.characters.split {$0 == " "}

fullNameArr[0] // First
fullNameArr[1] // Last 
原文链接:https://www.f2er.com/swift/325862.html

猜你在找的Swift相关文章