在Swift 3.0中使用String.range [英] Usage of String.range in Swift 3.0
本文介绍了在Swift 3.0中使用String.range的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
}
此代码提取 substring
我搜索了Internet,发现 rangeOfString
更改为 range()
。
This code extracts substring
between backslashes and dot com in Swift 2. I searched Internet and I found that rangeOfString
changed to range()
.
但是我仍然无法使代码在Swift 3.0中运行。你能帮助我吗?
But still I could not make the code work in Swift 3.0. Could you help me ?
编辑:我正在使用swift 3 07-25 build。
edit : I'm using swift 3 07-25 build.
推荐答案
在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中使用String.range的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文