String contains letters

流过昼夜 提交于 2019-12-24 03:06:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!