问题
I'm wondering if there is a way to check if a string is containing a letter.
If there is an NSString = @"street 12"
I want it to return YES
and if NSString = @"12.12,23.23"
I want it to return NO
Is there a method that handles this made by Apple or do I have to make it myself?
回答1:
Here's one way:
NSString *someString = ... // the string to check
NSRange match = [someString rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:NSMakeRange(0, someString.length)];
if (match.location != NSNotFound) {
// someString has a letter in it
}
回答2:
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ"];
if ([str rangeOfCharacterFromSet:set].location != NSNotFound) {
return YES;
}
return NO;
来源:https://stackoverflow.com/questions/16866879/string-contains-letters