Advertisements
NSCharacterSet by example
Creating a Standard Character Set
+ alphanumericCharacterSet
Returns a character set containing the characters in the categories Letters, Marks, and Numbers.
Example
unichar eezyUnichar1 = [@"a!1" characterAtIndex:0]; unichar eezyUnichar2 = [@"a!1" characterAtIndex:1]; unichar eezyUnichar3 = [@"a!1" characterAtIndex:2]; NSCharacterSet *characterSet = [NSCharacterSet alphanumericCharacterSet]; NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar3]);
Output
2014-04-09 04:33:44.196 iOS-Tutorial[2803:a0b] 1 2014-04-09 04:33:44.197 iOS-Tutorial[2803:a0b] 0 2014-04-09 04:33:44.197 iOS-Tutorial[2803:a0b] 1
+ capitalizedLetterCharacterSet
Returns a character set containing the characters in the category of Titlecase Letters.
Example
NSCharacterSet *characterSet = [NSCharacterSet capitalizedLetterCharacterSet];
+ controlCharacterSet
Returns a character set containing the characters in the categories of Control or Format Characters.
Example
unichar eezyUnichar1 = [@"\a@1" characterAtIndex:0]; unichar eezyUnichar2 = [@"A@1" characterAtIndex:1]; unichar eezyUnichar3 = [@"A@1" characterAtIndex:2]; NSCharacterSet *characterSet = [NSCharacterSet controlCharacterSet]; NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar3]);
Output
2014-04-09 05:05:37.906 iOS-Tutorial[3009:a0b] 1 2014-04-09 05:05:37.907 iOS-Tutorial[3009:a0b] 0 2014-04-09 05:05:37.907 iOS-Tutorial[3009:a0b] 0
+ decimalDigitCharacterSet
Returns a character set containing the characters in the category of Decimal Numbers.
Example
unichar eezyUnichar1 = [@"\a@1" characterAtIndex:0]; unichar eezyUnichar2 = [@"A@1" characterAtIndex:1]; unichar eezyUnichar3 = [@"A@1" characterAtIndex:2]; NSCharacterSet *characterSet = [NSCharacterSet decimalDigitCharacterSet]; NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar3]);
Output
2014-04-09 05:08:02.528 iOS-Tutorial[3036:a0b] 0 2014-04-09 05:08:02.529 iOS-Tutorial[3036:a0b] 0 2014-04-09 05:08:02.530 iOS-Tutorial[3036:a0b] 1
+ decomposableCharacterSet
Returns a character set containing all individual Unicode characters that can also be represented as composed character sequences.
Example
NSCharacterSet *characterSet = [NSCharacterSet decomposableCharacterSet];
+ illegalCharacterSet
Returns a character set containing values in the category of Non-Characters or that have not yet been defined in version 3.2 of the Unicode standard.
Example
NSCharacterSet *characterSet = [NSCharacterSet illegalCharacterSet];
+ letterCharacterSet
Returns a character set containing the characters in the categories Letters and Marks.
Example
unichar eezyUnichar1 = [@"A@1" characterAtIndex:0]; unichar eezyUnichar2 = [@"A@1" characterAtIndex:1]; unichar eezyUnichar3 = [@"A@1" characterAtIndex:2]; NSCharacterSet *characterSet = [NSCharacterSet letterCharacterSet]; NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar3]);
Output
2014-04-09 05:16:19.933 iOS-Tutorial[3098:a0b] 1 2014-04-09 05:16:19.935 iOS-Tutorial[3098:a0b] 0 2014-04-09 05:16:19.935 iOS-Tutorial[3098:a0b] 0
+ lowercaseLetterCharacterSet
Returns a character set containing the characters in the category of Lowercase Letters.
Example
NSRange lowercaseCharRange = [@"abc" rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]]; NSLog(@"%d",lowercaseCharRange.location!=NSNotFound);
Output
2014-04-09 05:21:26.737 iOS-Tutorial[3152:a0b] 1
+ newlineCharacterSet
Returns a character set containing the newline characters.
Example
NSRange newlineRange = [@"Eezy Tutorials\n" rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]]; NSLog(@"%d",newlineRange.location!=NSNotFound);
Output
2014-04-09 05:30:19.539 iOS-Tutorial[3320:a0b] 1
+ nonBaseCharacterSet
Returns a character set containing the characters in the category of Marks.
Example
NSCharacterSet *characterSet = [NSCharacterSet nonBaseCharacterSet];
+ punctuationCharacterSet
Returns a character set containing the characters in the category of Punctuation.
Example
unichar eezyUnichar1 = [@"A,;" characterAtIndex:0]; unichar eezyUnichar2 = [@"A,;" characterAtIndex:1]; unichar eezyUnichar3 = [@"A,;" characterAtIndex:2]; NSCharacterSet *characterSet = [NSCharacterSet punctuationCharacterSet]; NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar3]);
Output
2014-04-09 05:32:38.123 iOS-Tutorial[3340:a0b] 0 2014-04-09 05:32:38.124 iOS-Tutorial[3340:a0b] 1 2014-04-09 05:32:38.124 iOS-Tutorial[3340:a0b] 1
+ symbolCharacterSet
Returns a character set containing the characters in the category of Symbols.
Example
unichar eezyUnichar1 = [@"$,;" characterAtIndex:0]; unichar eezyUnichar2 = [@"$,;" characterAtIndex:1]; unichar eezyUnichar3 = [@"$,;" characterAtIndex:2]; NSCharacterSet *characterSet = [NSCharacterSet symbolCharacterSet]; NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]); NSLog(@"%d",[characterSet characterIsMember:eezyUnichar3]);
Output
2014-04-09 05:33:47.694 iOS-Tutorial[3365:a0b] 1 2014-04-09 05:33:47.694 iOS-Tutorial[3365:a0b] 0 2014-04-09 05:33:47.694 iOS-Tutorial[3365:a0b] 0
+ uppercaseLetterCharacterSet
Returns a character set containing the characters in the categories of Uppercase Letters and Titlecase Letters.
Example
NSCharacterSet *characterSet = [NSCharacterSet capitalizedLetterCharacterSet]; NSRange uppercaseRange = [@"Eezy Tutorials" rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]]; NSLog(@"%d",uppercaseRange.location!=NSNotFound);
Output
2014-04-09 05:28:43.767 iOS-Tutorial[3290:a0b] 1
+ whitespaceAndNewlineCharacterSet
Returns a character set containing Unicode General Category Z*, U000A ~ U000D, and U0085.
Example
NSLog(@"%@",[@"\nEezy Tutorials Website \n " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]);
Output
2014-04-09 05:45:27.725 iOS-Tutorial[3495:a0b] Eezy Tutorials Website
+ whitespaceCharacterSet
Returns a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009).
Example
NSLog(@"%@",[@" Eezy Tutorials Website " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]);
Output
2014-04-09 05:46:03.709 iOS-Tutorial[3505:a0b] Eezy Tutorials Website
Advertisements