最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 3035浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.213 seconds, using 22.11MB memory