Adding and Removing Characters

- addCharactersInRange:

Adds to the receiver the characters whose Unicode values are in a given range.

Example

unichar eezyUnichar1 = [@"a!1" characterAtIndex:0];
unichar eezyUnichar2 = [@"a!1" characterAtIndex:1];
NSMutableCharacterSet *characterSet =[[NSMutableCharacterSet alloc]init];
[characterSet addCharactersInRange:NSMakeRange(21, 14)];
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]);
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]);
		

Output

2014-04-12 19:48:55.507 iOS-Tutorial[1716:a0b] 0
2014-04-12 19:48:55.508 iOS-Tutorial[1716:a0b] 1
		

- removeCharactersInRange:

Removes from the receiver the characters whose Unicode values are in a given range.

Example

unichar eezyUnichar1 = [@"a!1" characterAtIndex:0];
unichar eezyUnichar2 = [@"a!1" characterAtIndex:1];
NSMutableCharacterSet *characterSet =[[NSMutableCharacterSet alloc]init];
[characterSet addCharactersInRange:NSMakeRange(21, 14)];
[characterSet removeCharactersInRange:NSMakeRange(25, 10)];
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]);
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]);
		

Output

2014-04-12 19:50:10.017 iOS-Tutorial[1730:a0b] 0
2014-04-12 19:50:10.019 iOS-Tutorial[1730:a0b] 0
		

- addCharactersInString:

Adds to the receiver the characters in a given string.

Example

unichar eezyUnichar1 = [@"a!1" characterAtIndex:0];
unichar eezyUnichar2 = [@"a!1" characterAtIndex:1];
NSMutableCharacterSet *characterSet =[[NSMutableCharacterSet alloc]init];
[characterSet addCharactersInString:@"!@#$%^&*()_+"];
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]);
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]);
		

Output

2014-04-12 19:51:25.842 iOS-Tutorial[1755:a0b] 0
2014-04-12 19:51:25.844 iOS-Tutorial[1755:a0b] 1
		

- removeCharactersInString:

Removes from the receiver the characters in a given string.

Example

unichar eezyUnichar1 = [@"a!1" characterAtIndex:0];
unichar eezyUnichar2 = [@"a@1" characterAtIndex:1];
NSMutableCharacterSet *characterSet =[[NSMutableCharacterSet alloc]init];
[characterSet addCharactersInString:@"!@#$%^&*()_+"];
[characterSet addCharactersInString:@"!"];
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]);
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]);
		

Output

2014-04-12 19:52:31.182 iOS-Tutorial[1776:a0b] 0
2014-04-12 19:52:31.188 iOS-Tutorial[1776:a0b] 1
		

Combining Character Sets

- formIntersectionWithCharacterSet:

Modifies the receiver so it contains only characters that exist in both the receiver and otherSet.

Example

unichar eezyUnichar1 = [@"!!1" characterAtIndex:0];
unichar eezyUnichar2 = [@"a@1" characterAtIndex:1];
NSMutableCharacterSet *characterSet =[[NSMutableCharacterSet alloc]init];
[characterSet addCharactersInString:@"!@#$%^&*()_+"];
[characterSet formIntersectionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]);
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]);
		

Output

2014-04-12 19:54:29.244 iOS-Tutorial[1820:a0b] 1
2014-04-12 19:54:29.245 iOS-Tutorial[1820:a0b] 1
		

- formUnionWithCharacterSet:

Modifies the receiver so it contains all characters that exist in either the receiver or otherSet.

Example

unichar eezyUnichar1 = [@"!!1" characterAtIndex:0];
unichar eezyUnichar2 = [@"a@1" characterAtIndex:1];
NSMutableCharacterSet *characterSet =[[NSMutableCharacterSet alloc]init];
[characterSet addCharactersInString:@"!@#$%^&*()_+"];
[characterSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]);
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]);
		

Output

2014-04-12 19:55:00.962 iOS-Tutorial[1832:a0b] 1
2014-04-12 19:55:00.964 iOS-Tutorial[1832:a0b] 1
		

Inverting a Character Set

- invert

Replaces all the characters in the receiver with all the characters it didn’t previously contain.

Example

unichar eezyUnichar1 = [@"!!1" characterAtIndex:0];
unichar eezyUnichar2 = [@"a@1" characterAtIndex:1];
NSMutableCharacterSet *characterSet =[[NSMutableCharacterSet alloc]init];
[characterSet addCharactersInString:@"!@#$%^&*()_+"];
[characterSet invert];
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar1]);
NSLog(@"%d",[characterSet characterIsMember:eezyUnichar2]);
		

Output

2014-04-12 19:55:35.821 iOS-Tutorial[1844:a0b] 0
2014-04-12 19:55:35.823 iOS-Tutorial[1844:a0b] 0
		

Advertisements