对于Swift中的字符,isDigit()的替换是什么?

时间:2014-10-14 02:10:17

标签: swift

this post中所述,在Xcode 6 Beta 4之前,可以使用c.isDigit()c.isAlpha()来查找c : Character是数字还是数字。帖子提到这被删除,因为它只对ASCII字符有效。

我的问题是,什么是替代品?如果没有使用带有字母数字选项的switch语句设置函数,我该如何在数字上测试一个字符?

6 个答案:

答案 0 :(得分:62)

"问题"是Swift字符不直接对应Unicode 代码点,但代表一个"扩展的字形集群"哪个可以包括 多个Unicode标量。例如

let c : Character = "" // REGIONAL INDICATOR SYMBOL LETTERS US

实际上是一系列两个 Unicode标量。

如果我们忽略这个事实,那么你可以检索的初始 Unicode标量 字符(比较How can I get the Unicode code point(s) of a Character?)并在字符集中测试其成员资格:

let c : Character = "5"

let s = String(c).unicodeScalars
let uni = s[s.startIndex]

let digits = NSCharacterSet.decimalDigitCharacterSet()
let isADigit = digits.longCharacterIsMember(uni.value)

这会返回" true"对于角色" 0" ......" 9",但实际上是所有 "decimal digit category"的Unicode标量,例如:

let c1 : Character = "৯" // BENGALI DIGIT NINE U+09EF
let c2 : Character = "" // MATHEMATICAL DOUBLE-STRUCK DIGIT ONE U+1D7D9

如果您只关心(ASCII)数字" 0" ..." 9",那么最简单的方法可能是:

if c >= "0" && c <= "9" { }

或使用范围:

if "0"..."9" ~= c { }

答案 1 :(得分:8)

Swift 3似乎更容易一些:

let str = "abcdef12345"
let digitSet = CharacterSet.decimalDigits

for ch in str.unicodeScalars {
    if digitSet.contains(ch) {
        // is digit!
    }
}

答案 2 :(得分:4)

关于 Swift 3

根据伟大的马丁R答案,对于谁有新的快速问题:

取代:

let digits = NSCharacterSet.decimalDigits as NSCharacterSet

使用:

{{1}}

答案 3 :(得分:4)

对于单个角色:

CharacterSet.decimalDigits.contains(string.unicodeScalars.first!)

答案 4 :(得分:3)

使用Swift 5,您可以根据需要选择以下方式之一来解决您的问题。


#1。使用Character的{​​{3}}属性

字符具有一个名为isNumber的属性。 isNumber具有以下声明:

var isNumber: Bool { get }
  

一个布尔值,指示此字符是否代表数字。

下面的Playground示例代码显示了如何使用isNumber检查字符是否代表数字:

let character: Character = "9"
print(character.isNumber) // true
let character: Character = "½"
print(character.isNumber) // true
let character: Character = "④"
print(character.isNumber) // true
let character: Character = "1⃣"
print(character.isNumber) // true
let character: Character = "1️⃣"
print(character.isNumber) // true
let character: Character = "৯"
print(character.isNumber) // true
let character: Character = ""
print(character.isNumber) // true
let character: Character = "F"
print(character.isNumber) // false

#2。使用Character的{​​{3}}属性

如果要检查字符是否代表整数,可以使用Character的{​​{1}}属性:

isWholeNumber
let character: Character = "9"
print(character.isWholeNumber) // true
let character: Character = "½"
print(character.isWholeNumber) // false
let character: Character = "④"
print(character.isWholeNumber) // true
let character: Character = "1⃣"
print(character.isWholeNumber) // false
let character: Character = "1️⃣"
print(character.isWholeNumber) // false
let character: Character = "৯"
print(character.isWholeNumber) // true
let character: Character = ""
print(character.isWholeNumber) // true

#3。使用let character: Character = "F" print(character.isWholeNumber) // false 的{​​{3}}属性和Unicode.Scalar.Properties

下面的Playground示例代码显示了如何使用Unicode.GeneralCategory.decimalNumbergeneralCategory检查字符的第一个Unicode标量是否为十进制数字:

Unicode.GeneralCategory.decimalNumber
let character: Character = "9"
let scalar = character.unicodeScalars.first! // DIGIT NINE
print(scalar.properties.generalCategory == .decimalNumber) // true
let character: Character = "½"
let scalar = character.unicodeScalars.first! // VULGAR FRACTION ONE HALF
print(scalar.properties.generalCategory == .decimalNumber) // false
let character: Character = "④"
let scalar = character.unicodeScalars.first! // CIRCLED DIGIT FOUR
print(scalar.properties.generalCategory == .decimalNumber) // false
let character: Character = "1⃣"
let scalar = character.unicodeScalars.first! // DIGIT ONE
print(scalar.properties.generalCategory == .decimalNumber) // true
let character: Character = "1️⃣"
let scalar = character.unicodeScalars.first! // DIGIT ONE
print(scalar.properties.generalCategory == .decimalNumber) // true
let character: Character = "৯"
let scalar = character.unicodeScalars.first! // BENGALI DIGIT NINE
print(scalar.properties.generalCategory == .decimalNumber) // true
let character: Character = ""
let scalar = character.unicodeScalars.first! // MATHEMATICAL DOUBLE-STRUCK DIGIT ONE
print(scalar.properties.generalCategory == .decimalNumber) // true

#4。使用let character: Character = "F" let scalar = character.unicodeScalars.first! // LATIN CAPITAL LETTER F print(scalar.properties.generalCategory == .decimalNumber) // false 的{​​{3}}属性和Unicode.Scalar.Properties

类似地,您可以使用Unicode.GeneralCategory.otherNumbergeneralCategory检查字符的第一个Unicode标量与Unicode标准中的类别 Other_Number 相对应:

Unicode.GeneralCategory.otherNumber
let character: Character = "9"
let scalar = character.unicodeScalars.first!
print(scalar.properties.generalCategory == .otherNumber) // false
let character: Character = "½"
let scalar = character.unicodeScalars.first!
print(scalar.properties.generalCategory == .otherNumber) // true
let character: Character = "④"
let scalar = character.unicodeScalars.first!
print(scalar.properties.generalCategory == .otherNumber) // true
let character: Character = "1⃣"
let scalar = character.unicodeScalars.first!
print(scalar.properties.generalCategory == .otherNumber) // false
let character: Character = "1️⃣"
let scalar = character.unicodeScalars.first!
print(scalar.properties.generalCategory == .otherNumber) // false
let character: Character = "৯"
let scalar = character.unicodeScalars.first!
print(scalar.properties.generalCategory == .otherNumber) // false
let character: Character = ""
let scalar = character.unicodeScalars.first!
print(scalar.properties.generalCategory == .otherNumber) // false

#5。使用let character: Character = "F" let scalar = character.unicodeScalars.first! print(scalar.properties.generalCategory == .otherNumber) // false 的{​​{3}}属性

或者,您可以导入Foundation并检查CharacterSet是否包含字符的第一个Unicode标量:

CharacterSet.decimalDigits
import Foundation

let character: Character = "9"
let scalar = character.unicodeScalars.first!
print(CharacterSet.decimalDigits.contains(scalar)) // true
import Foundation

let character: Character = "½"
let scalar = character.unicodeScalars.first!
print(CharacterSet.decimalDigits.contains(scalar)) // false
import Foundation

let character: Character = "④"
let scalar = character.unicodeScalars.first!
print(CharacterSet.decimalDigits.contains(scalar)) // false
import Foundation

let character: Character = "1⃣"
let scalar = character.unicodeScalars.first!
print(CharacterSet.decimalDigits.contains(scalar)) // true
import Foundation

let character: Character = "1️⃣"
let scalar = character.unicodeScalars.first!
print(CharacterSet.decimalDigits.contains(scalar)) // true
import Foundation

let character: Character = "৯"
let scalar = character.unicodeScalars.first!
print(CharacterSet.decimalDigits.contains(scalar)) // true
import Foundation

let character: Character = ""
let scalar = character.unicodeScalars.first!
print(CharacterSet.decimalDigits.contains(scalar)) // true

#6。使用import Foundation let character: Character = "F" let scalar = character.unicodeScalars.first! print(CharacterSet.decimalDigits.contains(scalar)) // false 的{​​{3}}

Apple文档中关于Unicode.Scalar.Properties的声明:

  

对于表示数字的标量,numericType是标量的数字类型。对于所有其他标量,此属性为numericType

以下示例代码显示给定字符的第一个标量的可能数字类型(nildecimaldigit):

numeric
let character: Character = "9"
let scalar = character.unicodeScalars.first!
print(scalar.properties.numericType) // Optional(Swift.Unicode.NumericType.decimal)
let character: Character = "½"
let scalar = character.unicodeScalars.first!
print(scalar.properties.numericType) // Optional(Swift.Unicode.NumericType.numeric)
let character: Character = "④"
let scalar = character.unicodeScalars.first!
print(scalar.properties.numericType) // Optional(Swift.Unicode.NumericType.digit)
let character: Character = "1⃣"
let scalar = character.unicodeScalars.first!
print(scalar.properties.numericType) // Optional(Swift.Unicode.NumericType.decimal)
let character: Character = "1️⃣"
let scalar = character.unicodeScalars.first!
print(scalar.properties.numericType) // Optional(Swift.Unicode.NumericType.decimal)
let character: Character = "৯"
let scalar = character.unicodeScalars.first!
print(scalar.properties.numericType) // Optional(Swift.Unicode.NumericType.decimal)
let character: Character = ""
let scalar = character.unicodeScalars.first!
print(scalar.properties.numericType) // Optional(Swift.Unicode.NumericType.decimal)

答案 5 :(得分:0)

我创建了我的var来检查字符是否为数字。

var isNumber : Bool {
    get{
        return !self.isEmpty && self.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) == nil
    }
}

//使用

var initial = "1"
if let isNum = initial.isNumber, isNum { // true
    initial = "#"
}