用NSPredicate在NSString中查詢數字:
NSString *test = @"[0-9]";
NSString *testString = @"ab9";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ CONTAINS[c] %@)", test,testString];
BOOL bResult = [predicate evaluateWithObject:testString]
但是只搜索到到了開始的數字,我也試過@"[0-9]+"和@"[0-9]*"這樣,但是都得不到正確結果
NSCharacterSet *set= [NSCharacterSet alphanumericCharacterSet];
if ([string rangeOfCharacterFromSet:[set invertedSet]].location == NSNotFound) {
// contains A-Z,a-z, 0-9
} else {
// invalid
}
肯定解決