Created
November 17, 2015 15:30
-
-
Save samuelbeek/b9972aadc0fcc792eeee to your computer and use it in GitHub Desktop.
Check if string contains number or whitespace
This file contains hidden or 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 { | |
func containsNumbers() -> Bool { | |
// check if there's a range for a number | |
let range = self.rangeOfCharacterFromSet(.decimalDigitCharacterSet()) | |
// range will be nil if no whitespace is found | |
if let _ = range { | |
return true | |
} else { | |
return false | |
} | |
} | |
func containsWhiteSpace() -> Bool { | |
// check if there's a range for a whitespace | |
let range = self.rangeOfCharacterFromSet(.whitespaceCharacterSet()) | |
// range will be nil if no whitespace is found | |
if let _ = range { | |
return true | |
} else { | |
return false | |
} | |
} | |
} | |
let string = "h21312312al112312lo" | |
print(string.containsNumbers()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And make them just: