String.range 在 Swift 3.0 中的用法
Usage of String.range in Swift 3.0
let us = "http://example.com"
let range = us.rangeOfString("(?<=://)[^.]+(?=.com)", options:.RegularExpressionSearch)
if range != nil {
let found = us.substringWithRange(range!)
print("found: \(found)") // found: example
}
这段代码提取了Swift中反斜杠和.com之间的substring
2.我在网上搜索了一下,发现rangeOfString
变成了range()
。
但我仍然无法使代码在 Swift 3.0 中工作。你能帮帮我吗?
编辑:我正在使用 swift 3 07-25 版本。
在 swift 3.0 rangeOfString
中,语法更改如下。
let us = "http://example.com"
let range = us.range(of:"(?<=://)[^.]+(?=.com)", options:.regularExpression)
if range != nil {
let found = us.substring(with: range!)
print("found: \(found)") // found: example
}
在最新的 swift 3.0 中使用 Xcode 8 Beta 6(SDK 的最新更新):
let us = "http://example.com"
let range = us.range(of: "(?<=://)[^.]+(?=.com)", options: .regularExpression)
if range != nil {
let found = us.substring(with: range!)
print("found: \(found)") // found: example
}
let us = "http://example.com"
let range = us.rangeOfString("(?<=://)[^.]+(?=.com)", options:.RegularExpressionSearch)
if range != nil {
let found = us.substringWithRange(range!)
print("found: \(found)") // found: example
}
这段代码提取了Swift中反斜杠和.com之间的substring
2.我在网上搜索了一下,发现rangeOfString
变成了range()
。
但我仍然无法使代码在 Swift 3.0 中工作。你能帮帮我吗?
编辑:我正在使用 swift 3 07-25 版本。
在 swift 3.0 rangeOfString
中,语法更改如下。
let us = "http://example.com"
let range = us.range(of:"(?<=://)[^.]+(?=.com)", options:.regularExpression)
if range != nil {
let found = us.substring(with: range!)
print("found: \(found)") // found: example
}
在最新的 swift 3.0 中使用 Xcode 8 Beta 6(SDK 的最新更新):
let us = "http://example.com"
let range = us.range(of: "(?<=://)[^.]+(?=.com)", options: .regularExpression)
if range != nil {
let found = us.substring(with: range!)
print("found: \(found)") // found: example
}