Friday, June 14, 2013

NSCharacterSet alphanumericCharacterSet example in Objective C (iOS).


NSCharacterSet alphanumericCharacterSet

Returns a character set containing the characters in the categories Letters, Marks, and Numbers.

+ (id)alphanumericCharacterSet

Return Value
A character set containing the characters in the categories Letters, Marks, and Numbers.

Discussion of [NSCharacterSet alphanumericCharacterSet]
Informally, this set is the set of all characters used as basic units of alphabets, syllabaries, ideographs, and digits.

NSCharacterSet alphanumericCharacterSet example.
NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString@"_"];
[_alnum formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];

Example of [NSCharacterSet alphanumericCharacterSet].
NSCharacterSet *charactersToRemove =
[[ NSCharacterSet alphanumericCharacterSet ] invertedSet ];

NSString *trimmedReplacement =
[[ someString componentsSeparatedByCharactersInSet:charactersToRemove ]
 componentsJoinedByString:@"" ];

NSCharacterSet alphanumericCharacterSet example.
@implementation NSString (alphaOnly)

- (BOOL) isAlphaNumeric
   {
    NSCharacterSet *unwantedCharacters =
       [[NSCharacterSet alphanumericCharacterSet] invertedSet];

    return ([self rangeOfCharacterFromSet:unwantedCharacters].location == NSNotFound) ? YES : NO;
    }

@end

End of NSCharacterSet alphanumericCharacterSet example article.