Skip to content

Instantly share code, notes, and snippets.

@keehun
Last active December 10, 2018 20:21
Show Gist options
  • Save keehun/78da2bac4cae86a6f2e0b7c8f144c882 to your computer and use it in GitHub Desktop.
Save keehun/78da2bac4cae86a6f2e0b7c8f144c882 to your computer and use it in GitHub Desktop.
func isValidNumeral(input4: String) -> Bool {
let numbers = CharacterSet.decimalDigits
let rangeOfInvalidCharacters = input4.rangeOfCharacter(from: numbers.inverted)
return rangeOfInvalidCharacters == nil
}
let string1 = "qwerty12345"
let string2 = "qwerty"
let string3 = "12345"
print("\"\(string1)\" only contains numbers: \(isValidNumeral(input4: string1))")
/// "qwerty12345" only contains numbers: false
print("\"\(string2)\" only contains numbers: \(isValidNumeral(input4: string2))")
/// "qwerty" only contains numbers: false
print("\"\(string3)\" only contains numbers: \(isValidNumeral(input4: string3))")
/// "12345" only contains numbers: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment