Search code examples
comparisoncharacterswiftdigits

How to find out if letter is Alphanumeric or Digit in Swift


I want to count the number of letters, digits and special characters in the following string:

let phrase = "The final score was 32-31!"

I tried:

for tempChar in phrase {
    if (tempChar >= "a" && tempChar <= "z") {
       letterCounter++
    }
// etc.

but I'm getting errors. I tried all sorts of other variations on this - still getting error - such as:

could not find an overload for '<=' that accepts the supplied arguments


Solution

  • For Swift 5 see rustylepord's answer.

    Update for Swift 3:

    let letters = CharacterSet.letters
    let digits = CharacterSet.decimalDigits
    
    var letterCount = 0
    var digitCount = 0
    
    for uni in phrase.unicodeScalars {
        if letters.contains(uni) {
            letterCount += 1
        } else if digits.contains(uni) {
            digitCount += 1
        }
    }
    

    (Previous answer for older Swift versions)

    A possible Swift solution:

    var letterCounter = 0
    var digitCount = 0
    let phrase = "The final score was 32-31!"
    for tempChar in phrase.unicodeScalars {
        if tempChar.isAlpha() {
            letterCounter++
        } else if tempChar.isDigit() {
            digitCount++
        }
    }
    

    Update: The above solution works only with characters in the ASCII character set, i.e. it does not recognize Ä, é or ø as letters. The following alternative solution uses NSCharacterSet from the Foundation framework, which can test characters based on their Unicode character classes:

    let letters = NSCharacterSet.letterCharacterSet()
    let digits = NSCharacterSet.decimalDigitCharacterSet()
    
    var letterCount = 0
    var digitCount = 0
    
    for uni in phrase.unicodeScalars {
        if letters.longCharacterIsMember(uni.value) {
            letterCount++
        } else if digits.longCharacterIsMember(uni.value) {
            digitCount++
        }
    }
    

    Update 2: As of Xcode 6 beta 4, the first solution does not work anymore, because the isAlpha() and related (ASCII-only) methods have been removed from Swift. The second solution still works.