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

[已解决]swift判断字符串是否全是数字

Swift crifan 4854浏览 0评论

swift string all digit

swift2 – Get integer value from string in swift – Stack Overflow

casting – Convert Int to String in Swift – Stack Overflow

swift string check all digit

How to check if a string contains an int? -Swift – Stack Overflow

nscharacterset – How can I check if a string contains letters in Swift? – Stack Overflow

What is the replacement for isDigit() for characters in Swift? – Stack Overflow

Determine if a string is numeric – Rosetta Code

然后用了:

    var isAllDigits: Bool {
        var isAll = true
       
        for eachChar in self.characters {
            if !((eachChar >= "1") && (eachChar <= "9")){
                isAll = false
                break
            }
        }
       
        return isAll
    }

完整代码:

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
    }
   
    var isAllLetters: Bool {
        var isAll = true
       
        for eachChar in self.characters {
            if !( ((eachChar >= "A") && (eachChar <= "Z")) || ((eachChar >= "a") && (eachChar <= "z")) ){
                isAll = false
                break
            }
        }
       
        return isAll
    }
    var isAllDigits: Bool {
        var isAll = true
       
        for eachChar in self.characters {
            if !((eachChar >= "1") && (eachChar <= "9")){
                isAll = false
                break
            }
        }
       
        return isAll
    }
}

示例:

func genHeaderText(headerName:String) -> String {
    print("\r\ngenHeaderText")
    var headerText = ""
   
    //if all chinese name, get last one cn char
    if headerName.isAllChineseCharacters {
        print("\(headerName) isAllChineseCharacters")
       headerText = getLastChar(headerName)
    }else if headerName.isAllLetters {
        print("\(headerName) isAllLetters")
        let firstTwoChar = (headerName as NSString).substringToIndex(2)
        print("firstTwoChar=\(firstTwoChar)")
        headerText = firstTwoChar.uppercaseString
    }else if headerName.isAllDigits {
        print("\(headerName) isAllDigits")
       headerText = (headerName as NSString).substringToIndex(2)
    }else if headerName.containsString(" "){
        print("\(headerName) contain empty space")
        let subStrArr:[String] = splitSingleStrToStrArr(headerName, seperatorStr: " ")
        print("subStrArr=\(subStrArr)")
        if subStrArr.count >= 2 {
            let firstStr = subStrArr[0]
            let secondStr = subStrArr[1]
            print("firstStr=\(firstStr), secondStr=\(secondStr)")
            let firstChar = getFirstChar(firstStr)
            let sencondChar = getFirstChar(secondStr)
            print("firstChar=\(firstChar), sencondChar=\(sencondChar)")
            let mergedTwoChar = firstChar + sencondChar
            print("mergedTwoChar=\(mergedTwoChar)")
            headerText = mergedTwoChar.uppercaseString
        }
    }else{
        let firstTwoChar = (headerName as NSString).substringToIndex(2)
        print("firstTwoChar=\(firstTwoChar)")
        headerText = firstTwoChar.uppercaseString
    }
   
    print("\(headerName) -> \(headerText)")
    return headerText
}
genHeaderText("2937518054189267")

输出:

genHeaderText
firstTwoChar=29
2937518054189267 -> 29

转载请注明:在路上 » [已解决]swift判断字符串是否全是数字

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.176 seconds, using 22.25MB memory