How to check if a string contains only alphanumeric characters in a C object?

I am working on a small iphone project and I will need to check if the entered username contains only alphanumeric characters? (AZ, az, 0-9 . How can I check it?

+44
string objective-c iphone alphanumeric
Nov 04 '09 at 3:58
source share
8 answers

If you do not want to introduce a regex library for this one task ...

 NSString *str = @"aA09"; NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet]; BOOL valid = [[str stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""]; 
+84
Nov 04 '09 at 4:29
source share

This will work:

 @implementation NSString (alphaOnly) - (BOOL) isAlphaNumeric { NSCharacterSet *unwantedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; return ([self rangeOfCharacterFromSet:unwantedCharacters].location == NSNotFound); } @end 
+59
Nov 04 '09 at 8:02
source share

You can use this regex library for ObjectiveC. Use the following regular expression to match:

 ^[a-zA-Z0-9]*$ 
+9
Nov 04 '09 at 4:04
source share

NSCharacterSet based NSCharacterSet do not give the results you would expect for Japanese, etc. text, often stating that they contain alphanumeric characters. Test execution comes down to the fact that "there are only letters or numbers" and Japanese (etc.) characters are considered "Letters".

If you are trying to check Latin characters in a foreign language (for example, in Japanese), then the answer is from How to determine if NSString is Latin? "may I help:

 BOOL isLatin = [myString canBeConvertedToEncoding:NSISOLatin1StringEncoding]; 

NSASCIIStringEncoding can also be used instead of NSISOLatin1StringEncoding to further limit valid characters. After that, you can also test using NSCharacterSet to exclude special characters such as !, #, etc.

+8
Jul 23 2018-12-12T00:
source share

I have done some pretty extensive performance testing, and there are a few considerations to consider when choosing a method for checking alphanumeric strings. First, of course, you may not even care about performance. If your application rarely checks strings, or perhaps even does it once, any method that gives you the behavior you want is fine. In addition, here are my work results.

For custom character sets (e.g. alphanumeric characters, without Unicode characters or labels) this is the fastest to get started:

 NSCharacterSet *alphanumericSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"]; NSString *result = [self stringByTrimmingCharactersInSet:alphanumericSet]; return [result isEqualToString:@""]; 

If you agree using a pre-computed character set, such as [NSCharacterSet alphanumericCharacterSet] , this was the fastest:

 NSCharacterSet *alphanumericSet = [NSCharacterSet alphanumericCharacterSet]; alphanumericSet = alphanumericSet.invertedSet; NSRange range = [self rangeOfCharacterFromSet:alphanumericSet]; return (range.location == NSNotFound); 

Caching a character set in a static variable using dispatch_once may help a little if you re-run these checks. In this case, if you are sure that you can understand the initial compilation time, using a regular expression is actually the fastest for custom character sets:

 static NSRegularExpression *alphanumericRegex; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ alphanumericRegex = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z0-9]*$" options:NSRegularExpressionCaseInsensitive error:nil]; }); NSUInteger numberOfMatches = [alphanumericRegex numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)]; return (numberOfMatches == 1); 

If you do not want to use the regular expression, the custom version of the cached rangeOfCharacterFromSet limits the caching method stringByTrimmingCharactersInCharacterSet: ::

 static NSCharacterSet *alphanumericSet; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ alphanumericSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"]; alphanumericSet = alphanumericSet.invertedSet; }); NSRange range = [self rangeOfCharacterFromSet:alphanumericSet]; return (range.location == NSNotFound); 

For pre-computed sets, the rangeOfCharacterFromSet: caching method was again the fastest:

 static NSCharacterSet *alphanumericSet; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ alphanumericSet = [NSCharacterSet alphanumericCharacterSet]; alphanumericSet = alphanumericSet.invertedSet; }); NSRange range = [self rangeOfCharacterFromSet:alphanumericSet]; return (range.location == NSNotFound); 

And for each information, the isSupersetOfSet: method was the slowest, whether in the cache or not. Looks like isSupersetOfSet: pretty slow.

 NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:self]; NSCharacterSet *alphanumericSet = [NSCharacterSet alphanumericCharacterSet]; return [alphanumericSet isSupersetOfSet:stringSet]; 

I have not tested the basic functions of CFCharacterSet.

+5
Oct 27 '14 at 20:16
source share
 - (BOOL)isAlphaNumeric { NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'. "]; s = [s invertedSet]; NSRange r = [self rangeOfCharacterFromSet:s]; if (r.location == NSNotFound) { return NO; } else { return YES; } } 

Is it possible to add / subtract new characters, for example, spaces

PS This method can be copied / pasted into the NSString category

+3
Apr 26 '14 at 23:45
source share

I really like the RegexKit Lite Framework. It uses the ICU regex library, which is already included in OSX and is safe for Unicode.

 NSString *str = @"testString"; [str isMatchedByRegex:@"^[a-zA-Z0-9]*$"]; // strict ASCII-match [str isMatchedByRegex:@"^[\p{L}\p{N}]*$"]; // unicode letters and numbers match 
+2
Nov 04 '09 at 16:46
source share

You can use the NSString regex features introduced in iOS 3.2:

 - (BOOL)isAlphanumeric:(NSString *)string { return [string rangeOfString:@"^[a-zA-Z0-9]+$" options:NSRegularExpressionSearch].location != NSNotFound; } 
0
Mar 14 '16 at 17:08
source share



All Articles