截取字符串
swift5.x
var str = "01234567890"
let start = str.index(str1.startIndex, offsetBy: 1)
let end = str.index(str1.startIndex,offsetBy:6)
// str = str.substring(with: start..<end) //swift4 已经废除
print("start - end str = \(String(str[start..<end]))")
print(" - end str = \(String(str[...end]))")
print("start - str = \(String(str[start...]))")
let newStr = str[…<end] 字符串开始到end位置 不包含end 打印 012345
let newStr = str[start…] 从start开始字符换末尾 包含start 打印 1234567890
let newStr = String(str[start…<end]) 包含start 不包含end 打印 12345
swift5.x 处理字符串并无没有像OC那样方便,可以将其分装成方法以供子弟调用
匹配前缀 hasPrefix(“需要匹配的字符串”) 匹配后缀 hasSuffix(“需要匹配的字符串”)
var str001:String = "zh_US"
var str002:String = "en_US"
print("前缀匹配>>>>>>\(str001.hasPrefix("zh_") ? "true":"false")")
print("前缀匹配>>>>>>\(str002.hasPrefix("en_") ? "true":"false")")
print("后缀匹配>>>>>>\(str001.hasSuffix("US") ? "true":"false")")
print("后缀匹配>>>>>>\(str002.hasSuffix("US") ? "true":"false")")
String(字符串) 转 Array(数组)
let str003:String = "a,b,c,d,e,f"
let list001:Array<Substring> = str003.split(separator: ",")
print(list001)
let str004:String = list001.joined(separator: ",")
print(str004)
打印
[“a”, “b”, “c”, “d”, “e”, “f”]
a,b,c,d,e,f