My problem is unary operator is not in Swift 3.0 Below is my snippet.
func checkCharacter(x: Character) {
if x >= "a" || x <="z"{
}
}
How to check that given character lies between a to z in swift 3.0
Try like this.
func checkCharacter(x: Character) {
if x >= "a" && x <= "z" {
print("Inside character range")
}
else {
print("Not inside character range")
}
}
checkCharacter(x: "c")
I would strongly recommend using a regex like this
let testStr = "a"
let matches = self.matchesForRegexInText(regex: "^[a-zA-Z]+$", text: testStr)
if matches.count > 0 {
//string contains only a-z or A-Z
}
func matchesForRegexInText(regex: String, text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matches(in: text,
options: [], range: NSMakeRange(0, nsString.length))
return results.map { nsString.substring(with: $0.range)}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
edit: I stole the code without mentioning it: Swift extract regex matches