在swift 3.0 以后,不能使用數字角標截取字符串了,手頭的需求是將“<a rel="nofollow">新浪微博</a>” 標簽中的內容截取出來。
隨便翻了一些博客,沒有找到我想要的方法(肯定是我找的太少了。。。)
所以自己寫吧!
// 數據源
let source = "<a href=\"http://weibo.com\" rel=\"nofollow\">新浪微博</a>"
// 開始range
let st = source.range(of: ">")
// 結束range
let ed = source.range(of: "</a>")
// 截取
let result = source.substring(with: (st?.upperBound)!..<(ed?.lowerBound)!)
至于其他的截取,比如從開始截取或者從某一位置截取到最后都很簡單不多說了。(其實因為我是新手,怕自己也說不明白。。。)