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
Any clues?
有线索吗?
3 个解决方案
#1
49
Update for Swift 3:
更新迅速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)
(较早版本的Swift)
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:
更新:上面的解决方案只能在ASCII字符集字符,即它并不承认,e或ø信件。下面的备选解决方案使用Foundation框架中的NSCharacterSet,该框架可以根据它们的Unicode字符类测试字符:
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.
更新2:从Xcode 6 beta 4开始,第一个解决方案不再工作,因为isAlpha()和相关的(仅ascii)方法已经从Swift中删除。第二种方法仍然有效。
#2
4
Use the values of unicodeScalars
使用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)
#3
0
I've created a short extension for letter and digits count for a String
我为字母和数字计数创建了一个简短的扩展名
extension String {
var letterCount : Int {
return self.unicodeScalars.filter({ CharacterSet.letters.contains($0) }).count
}
var digitCount : Int {
return self.unicodeScalars.filter({ CharacterSet.decimalDigits.contains($0) }).count
}
}
or a function to get a count for any CharacterSet
you put in
或者是一个函数,用于获取输入的任何字符集的计数
extension String {
func characterCount(for set: CharacterSet) -> Int {
return self.unicodeScalars.filter({ set.contains($0) }).count
}
}
usage:
用法:
let phrase = "the final score is 23-13!"
let letterCount = phrase.characterCount(for: .letters)
#1
49
Update for Swift 3:
更新迅速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)
(较早版本的Swift)
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:
更新:上面的解决方案只能在ASCII字符集字符,即它并不承认,e或ø信件。下面的备选解决方案使用Foundation框架中的NSCharacterSet,该框架可以根据它们的Unicode字符类测试字符:
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.
更新2:从Xcode 6 beta 4开始,第一个解决方案不再工作,因为isAlpha()和相关的(仅ascii)方法已经从Swift中删除。第二种方法仍然有效。
#2
4
Use the values of unicodeScalars
使用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)
#3
0
I've created a short extension for letter and digits count for a String
我为字母和数字计数创建了一个简短的扩展名
extension String {
var letterCount : Int {
return self.unicodeScalars.filter({ CharacterSet.letters.contains($0) }).count
}
var digitCount : Int {
return self.unicodeScalars.filter({ CharacterSet.decimalDigits.contains($0) }).count
}
}
or a function to get a count for any CharacterSet
you put in
或者是一个函数,用于获取输入的任何字符集的计数
extension String {
func characterCount(for set: CharacterSet) -> Int {
return self.unicodeScalars.filter({ set.contains($0) }).count
}
}
usage:
用法:
let phrase = "the final score is 23-13!"
let letterCount = phrase.characterCount(for: .letters)