Created
February 23, 2020 20:43
-
-
Save foxicode/fe7fceda49e2a5c8184a40a09bea6a85 to your computer and use it in GitHub Desktop.
Swift String extension checking if String contains letters or digits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension String { | |
var containsOnlyDigits: Bool { | |
let notDigits = NSCharacterSet.decimalDigits.inverted | |
return rangeOfCharacter(from: notDigits, options: String.CompareOptions.literal, range: nil) == nil | |
} | |
var containsOnlyLetters: Bool { | |
let notLetters = NSCharacterSet.letters.inverted | |
return rangeOfCharacter(from: notLetters, options: String.CompareOptions.literal, range: nil) == nil | |
} | |
var isAlphanumeric: Bool { | |
let notAlphanumeric = NSCharacterSet.decimalDigits.union(NSCharacterSet.letters).inverted | |
return rangeOfCharacter(from: notAlphanumeric, options: String.CompareOptions.literal, range: nil) == nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks