【发布时间】:2017-07-04 07:08:37
【问题描述】:
我正在尝试使用 Swift 3 CharacterSet 过滤掉 String 中的字符,但我很早就被卡住了。 CharacterSet 有一个名为contains的方法
func contains(_ member: UnicodeScalar) -> Bool
测试 CharacterSet 中特定 UnicodeScalar 的成员资格。
但对此进行测试不会产生预期的行为。
let characterSet = CharacterSet.capitalizedLetters
let capitalAString = "A"
if let capitalA = capitalAString.unicodeScalars.first {
print("Capital A is \(characterSet.contains(capitalA) ? "" : "not ")in the group of capital letters")
} else {
print("Couldn't get the first element of capitalAString's unicode scalars")
}
我收到了Capital A is not in the group of capital letters,但我期望相反。
非常感谢。
【问题讨论】:
-
Martin 比我快,但该字符集不包含您要查找的大写字母 A:
["Dž", "Lj", "Nj", "Dz", "ᾈ", "ᾉ", "ᾊ", "ᾋ", "ᾌ", "ᾍ", "ᾎ", "ᾏ", "ᾘ", "ᾙ", "ᾚ", "ᾛ", "ᾜ", "ᾝ", "ᾞ", "ᾟ", "ᾨ", "ᾩ", "ᾪ", "ᾫ", "ᾬ", "ᾭ", "ᾮ", "ᾯ", "ᾼ", "ῌ", "ῼ"] -
虽然 Martin 的回答是第一个并且是正确的,但我认为您已经发现了问题的根源——我什至不知道大写字母是什么!
标签: swift