5

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.

user551353
  • 79
  • 1
  • 6
  • 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 Answers7

8

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]) { 
       ... 
    }
}
Jano
  • 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
6
NSCharacterSet *validChars = [NSCharacterSet alphanumericCharacterSet];
NSCharacterSet *invalidChars = [validChars invertedSet];

NSString *targetString = [[NSString alloc] initWithString: @"..."];
NSArray *components = [targetString componentsSeparatedByCharactersInSet:invalidChars];

NSString *resultString = [components componentsJoinedByString:@""];
Denis
  • 6,313
  • 1
  • 28
  • 27
5

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);
Monolo
  • 18,205
  • 17
  • 69
  • 103
2

You can use the C functions declared in ctype.h (included by default with Foundation). Be careful with multibyte characters though. Check the man pages.

char c = 'a';
if (isdigit(c)) {
    /* ... */
} else if (isalpha(c)) {
    /* ... */
}

/* or */
if (isalnum(c))
    /* ... */
sidyll
  • 57,726
  • 14
  • 108
  • 151
0

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?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

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:

http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html

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.

Farzad A
  • 578
  • 1
  • 5
  • 15
0

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.

Ali3n
  • 1,244
  • 6
  • 10