Friday, June 14, 2013

NSCharacterSet punctuationCharacterSet example in Objective C (iOS).


NSCharacterSet punctuationCharacterSet

Returns a character set containing the characters in the category of Punctuation.

+ (id)punctuationCharacterSet

Return Value
A character set containing the characters in the category of Punctuation.

Discussion of [NSCharacterSet punctuationCharacterSet]
Informally, this set is the set of all non-whitespace characters used to separate linguistic units in scripts, such as periods, dashes, parentheses, and so on.

NSCharacterSet punctuationCharacterSet example.
If you already know the characters that you want to remove.

NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"+-(),. "];
NSArray *arrayWithNumbers = [str componentsSeparatedByCharactersInSet:charSet];
NSString *numberStr = [arrayWithNumbers componentsJoinedByString:@""];
If you don't know the characters, you can use the different character sets available already

NSMutableCharacterSet *charSet = [NSMutableCharacterSet new];
[charSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
NSArray *arrayWithNumbers = [str componentsSeparatedByCharactersInSet:charSet];
NSString *numberStr = [arrayWithNumbers componentsJoinedByString:@""];

Example of [NSCharacterSet punctuationCharacterSet].
To check if a string has any characters belonging to a set, you simply do this:

NSString *testString = @"hello$%^";
NSRange r = [testString rangeOfCharactersFromSet:[NSCharacterSet punctuationCharacterSet]];
if (r.location != NSNotFound) {
    // the string contains a punctuation character
}
If you want to know all of the locations of the punctuation characters, just keep searching with a different input range. Something like:

NSRange searchRange = NSMakeRange(0, [testString length]);
while (searchRange.location != NSNotFound) {
    NSRange foundRange = [searchString rangeOrCharacterFromSet:[NSCharacterSet punctuationCharacterSet] options:0 range:searchRange];
    searchRange.location = foundRange.location + 1;
    searchRange.length = [testString length] - searchRange.location;
    if (foundRange.location != NSNotFound) {
        // found a character at foundRange.location
    }
}

NSCharacterSet punctuationCharacterSet example.
NSString *backgroundColorString = @"rgb(34, 34, 34)";

NSScanner *scanner = [NSScanner scannerWithString:backgroundColorString];
NSString *junk, *red, *green, *blue;
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&junk];
[scanner scanUpToCharactersFromSet:[NSCharacterSet punctuationCharacterSet] intoString:&red];
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&junk];
[scanner scanUpToCharactersFromSet:[NSCharacterSet punctuationCharacterSet] intoString:&blue];
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&junk];
[scanner scanUpToCharactersFromSet:[NSCharacterSet punctuationCharacterSet] intoString:&green];

UIColor *backgroundColor = [UIColor colorWithRed:red.intValue/255.0 green:green.intValue/255.0 blue:blue.intValue/255.0 alpha:1.0];

End of NSCharacterSet punctuationCharacterSet example article.