截取‘:’前面所有的字符串
let str = "https://jianshu.com"
let range: Range = str.range(of: ":")!
let location: Int = str.distance(from: str.startIndex, to: range.lowerBound)
let subStr = str.prefix(location)
print(subStr)
打印结果:https
截取‘:’前面所有的字符串(结果包含‘:’)
let str = "https://jianshu.com"
let range: Range = str.range(of: ":")!
let location: Int = str.distance(from: str.startIndex, to: range.upperBound)
let subStr = str.prefix(location)
print(subStr)
打印结果:https:
截取':'后面的所有字符串
let str = "https://jianshu.com"
let range: Range = str.range(of: ":")!
let location: Int = str.distance(from: str.startIndex, to: range.upperBound)
let subStr = str.suffix(str.count - location)
print(subStr)
打印结果://jianshu.com
截取':'后面的所有字符串(结果包含‘:’)
let str = "https://jianshu.com"
let range: Range = str.range(of: ":")!
let location: Int = str.distance(from: str.startIndex, to: range.lowerBound)
let subStr = str.suffix(str.count - location)
print(subStr)
打印结果:://jianshu.com
小白写文章,写的不好的地方欢迎指出,好让我提升一下,多谢