In objective-c, how would I check if a single character was either a letter or a number? I would like to eliminate all other characters.
-
possible duplicate of [Strip Non-Alphanumeric Characters from an NSString](http://stackoverflow.com/questions/1656410/strip-non-alphanumeric-characters-from-an-nsstring) – Sergey Kalinichenko Dec 13 '11 at 15:40
-
@dasblinkenlight Maybe, but we'd need to know better what "eliminate all other characters" means more clearly. Is it _eliminate from a string_, _eliminate from the test results_, or something else? – sidyll Dec 13 '11 at 15:46
7 Answers
To eliminate non letters:
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSCharacterSet *notLetters = [[NSCharacterSet characterSetWithCharactersInString:letters] invertedSet];
NSString *newString = [[string componentsSeparatedByCharactersInSet:notLetters] componentsJoinedByString:@""];
To check one character at a time:
for (int i = 0; i < [string length]; i++) {
unichar c = [string characterAtIndex:i];
if ([notLetters characterIsMember:c]) {
...
}
}

- 62,815
- 21
- 164
- 192
-
oops that should be alphanumeric (not letters only) so NSCharacterSet alphanumericCharacterSet. Still not bad idiom to work with an arbitrary set of characters. – Jano Dec 13 '11 at 15:52
NSCharacterSet *validChars = [NSCharacterSet alphanumericCharacterSet];
NSCharacterSet *invalidChars = [validChars invertedSet];
NSString *targetString = [[NSString alloc] initWithString: @"..."];
NSArray *components = [targetString componentsSeparatedByCharactersInSet:invalidChars];
NSString *resultString = [components componentsJoinedByString:@""];

- 6,313
- 1
- 28
- 27
Many ways to do it, here is one using character sets:
unichar ch = '5';
BOOL isLetter = [[NSCharacterSet letterCharacterSet] characterIsMember: ch];
BOOL isDigit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember: ch];
NSLog(@"'%C' is a letter: %d or a digit %d", ch, isLetter, isDigit);

- 18,205
- 17
- 69
- 103
Checking if a character is a (arabic) number:
NSString* text = [...];
unichar character = [text characterAtIndex:0];
BOOL isNumber = (character >= '0' && character <= '9');
It would be different if you would to know other numeric characters (indian numbers, japanese numbers etc.)
Whether character is a letter depends on what you mean by letter. ASCII letters? Unicode letters?

- 128,090
- 22
- 218
- 270
I'm not too familiar with obj-c, but this sounds like something that can be achieved using a regex pattern.
I did some searching and found this:
Try running a regex of "[0-9]" on each character to determine if it is a number.
Save the number to a temporary array, your array should end up with only the numbers.
I would type out the code...but as I said before, I'm not familiar with obj-c. I hope this helps though.

- 578
- 1
- 5
- 15
You can check them by comparing the ASCII value for number (0-9 ASCII ranged from 48-57) .. Also you can use NSScanner in Objective C to test for int or a char.

- 1,244
- 6
- 10