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
Create a regular expression to check string is alphanumeric or not as mentioned below: regex = "^ (?=.* [a-zA-Z]) (?=.* [0-9]) [A-Za-z0-9]+$"; Where: ^ represents the starting of the string. (?=.* [a-zA-Z]) represents the alphabets from a-z, A-Z. (?=.* [0-9]) represents any number from 0-9.
Note: It is recommended we use the isalpha () function to check whether a character is an alphabet or not. Did you find this article helpful? Sorry about that. How can we improve it?
This string contains all the alphabets from a-z, A-Z, but doesn’t contain any number from 0-9. Therefore, it is not an alphanumeric string. This string contains all the alphabets from a-z, A-Z, and the number from 0-9 along with some special symbols.
If you want to just check a string is only alphabets and Numbers then you can use below Regex Patter, Int Count = Regex.Matches (str,"^ [a-zA-Z0-9]+$").Count
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.
Use the values of unicodeScalars
let phrase = "The final score was 32-31!"
var letterCounter = 0, digitCounter = 0
for scalar in phrase.unicodeScalars {
let value = scalar.value
if (value >= 65 && value <= 90) || (value >= 97 && value <= 122) {++letterCounter}
if (value >= 48 && value <= 57) {++digitCounter}
}
println(letterCounter)
println(digitCounter)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With