public extension NSString {
func split(splitStr:NSString)->[NSString] {
let scanner = Scanner(string: self as String)
var arr:[NSString] = []
while (scanner.isAtEnd == false){
var subStr:NSString? = ""
scanner.scanUpTo(splitStr as String, into: &subStr)
arr.append(subStr!)
}
return arr
}
}
测试效果如图
java中的split函数是把字符串按特定字符分割成数组,对应的swift中的处理方法:可以使用global split函数处理,举例如下:
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count >1 ? fullNameArr[1] : nil
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)