如何在Swift中找出字母是字母数字还是数字

时间:2022-08-24 07:30:59

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

找不到接受提供的参数的'<='的重载

3 个解决方案

#1


50  

Update for Swift 3:

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)

(以前的旧版Swift版本的答案)

A possible Swift solution:

一个可能的Swift解决方案:

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字符集中的字符,即它不能将Ä,é或ø识别为字母。以下替代解决方案使用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开始,第一个解决方案不再起作用,因为已经从Swift中删除了isAlpha()和相关(仅限ASCII)方法。第二种解决方案仍然有效。

#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

或者一个函数来获取你输入的任何CharacterSet的计数

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


50  

Update for Swift 3:

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)

(以前的旧版Swift版本的答案)

A possible Swift solution:

一个可能的Swift解决方案:

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字符集中的字符,即它不能将Ä,é或ø识别为字母。以下替代解决方案使用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开始,第一个解决方案不再起作用,因为已经从Swift中删除了isAlpha()和相关(仅限ASCII)方法。第二种解决方案仍然有效。

#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

或者一个函数来获取你输入的任何CharacterSet的计数

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)