最新消息:20190529 VPS服务器已从腾讯云香港换为Vultr新加坡,主题仍用朋友推荐的大前端D8

[已解决]swift通过Range获取字符串中对应的值

Swift crifan 804浏览 0评论

搜:

swift string range

How To Find A Substring In Range of a Swift String

nsstring – NSRange to Range<String.Index> – Stack Overflow

swift中字符串截取方法(substring) – swift迷

最后用:

substringWithRange(chineseRangeIdx)

完整代码:

extension String {
    var containsChineseCharacters: Bool {
        return self.rangeOfString("\\p{Han}", options: .RegularExpressionSearch) != nil
    }
   
    var isAllChineseCharacters: Bool {
        var isAll = false
       
        if let chineseRangeIdx = self.rangeOfString("\\p{Han}.+", options: .RegularExpressionSearch) {
            print("chineseRangeIdx=\(chineseRangeIdx)")
            let chineseSubStr = self.substringWithRange(chineseRangeIdx)
            print("chineseSubStr=\(chineseSubStr)")
            if chineseSubStr == self {
                isAll = true
            }
        }
        return  isAll
    }
}

转载请注明:在路上 » [已解决]swift通过Range获取字符串中对应的值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
66 queries in 0.099 seconds, using 18.82MB memory