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