在Swift中,我如何检查String
是否是字母数字,即,它是否只包含一个或多个字母数字字符[a-zA-Z0-9]
,不包括带有变音符号的字母,例如é。
答案 0 :(得分:50)
extension String {
var isAlphanumeric: Bool {
return !isEmpty && range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil
}
}
"".isAlphanumeric // false
"abc".isAlphanumeric // true
"123".isAlphanumeric // true
"ABC123".isAlphanumeric // true
"iOS 9".isAlphanumeric // false
答案 1 :(得分:21)
extension String {
func isAlphanumeric() -> Bool {
return self.rangeOfCharacter(from: CharacterSet.alphanumerics.inverted) == nil && self != ""
}
func isAlphanumeric(ignoreDiacritics: Bool = false) -> Bool {
if ignoreDiacritics {
return self.range(of: "[^a-zA-Z0-9]", options: .regularExpression) == nil && self != ""
}
else {
return self.isAlphanumeric()
}
}
}
<强>用法:强>
"".isAlphanumeric() == false
"Hello".isAlphanumeric() == true
"Hello 2".isAlphanumeric() == false
"Hello3".isAlphanumeric() == true
"Français".isAlphanumeric() == true
"Français".isAlphanumeric(ignoreDiacritics: true) == false
这适用于英语以外的语言,允许使用è和á等变音字符。如果您想忽略这些字符,请使用标记“ignoreDiacritics: true
”。
答案 2 :(得分:12)
我觉得使用正则表达式所接受的答案是具体的,但却是一个相当重要的解决方案。您也可以在Swift 3.0中以这种方式检查它:
This is a repeated sentence This is a repeated sentence
答案 3 :(得分:3)
extension String {
/// Allows only `a-zA-Z0-9`
public var isAlphanumeric: Bool {
guard !isEmpty else {
return false
}
let allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
let characterSet = CharacterSet(charactersIn: allowed)
guard rangeOfCharacter(from: characterSet.inverted) == nil else {
return false
}
return true
}
}
XCTAssertFalse("".isAlphanumeric)
XCTAssertFalse("climate change".isAlphanumeric)
XCTAssertFalse("Poüet".isAlphanumeric)
XCTAssertTrue("Hawking2018".isAlphanumeric)
答案 4 :(得分:1)
此正则表达式用于检查该字符串是否包含至少1个字母+至少1个数字以及8个字符。
"^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"
答案 5 :(得分:1)
extension String {
var isAlphaNumeric: Bool {
let hasLetters = rangeOfCharacter(from: .letters, options: .numeric, range: nil) != nil
let hasNumbers = rangeOfCharacter(from: .decimalDigits, options: .literal, range: nil) != nil
let comps = components(separatedBy: .alphanumerics)
return comps.joined(separator: "").count == 0 && hasLetters && hasNumbers
}
}
答案 6 :(得分:0)
CharacterSet.alphanumerics
字符集的问题在于,它比[a-zA-Z0-9]
更宽容。它包含带变音符号的字母,东方阿拉伯数字等。
assert(["e", "E", "3"].allSatisfy({ CharacterSet.alphanumerics.contains($0) }))
assert(["ê", "É", "٣"].allSatisfy({ CharacterSet.alphanumerics.contains($0) }))
您可以仅使用特定的62个“字母数字”字符来构建自己的CharacterSet
:
extension CharacterSet {
static var alphanumeric62: CharacterSet {
return lowercase26.union(uppercase26).union(digits10)
}
static var lowercase26: CharacterSet { CharacterSet(charactersIn: "a"..."z") }
static var uppercase26: CharacterSet { CharacterSet(charactersIn: "A"..."Z") }
static var digits10: CharacterSet { CharacterSet(charactersIn: "0"..."9") }
}
assert(["e", "E", "3"].allSatisfy({ CharacterSet.alphanumeric62.contains($0) }))
assert(["ê", "É", "٣"].allSatisfy({ CharacterSet.alphanumeric62.contains($0) == false }))
然后针对该CharacterSet的 inverse 测试您的字符串:
guard "string".rangeOfCharacter(from: CharacterSet.alphanumeric62.inverted) == nil else {
fatalError()
}