截取‘:’前面所有的字符串
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)
打印結(jié)果:https
截取‘:’前面所有的字符串(結(jié)果包含‘:’)
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)
打印結(jié)果: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)
打印結(jié)果://jianshu.com
截取':'后面的所有字符串(結(jié)果包含‘:’)
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)
打印結(jié)果:://jianshu.com
小白寫文章镐侯,寫的不好的地方歡迎指出辟拷,好讓我提升一下,多謝