Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the Use of invertedSet method of NSCharacterSet

So as I work my way through understanding string methods, I came across this useful class NSCharacterSet

which is defined in this post quite well as being similar to a string excpet it is used for holding the char in an unordered set

What is differnce between NSString and NSCharacterset?

So then I came across the useful method invertedSet, and it bacame a little less clear what was happening exactly. Also I a read page a fter page on it, they all sort of glossed over the basics of what was happening and jumped into advanced explainations. So if you wanted to know what this is and why we use It SIMPLY put, it was not so easy instead you get statements like this from the apple documentation: "A character set containing only characters that don’t exist in the receiver." - and how do I use this exactly???

So here is what i understand to be the use. PLEASE provide in simple terms if I have explained this incorrectly.

Example Use:

Create a list of Characters in a NSCharacterSetyou want to limit a string to contain.

NSString *validNumberChars      = @"0123456789"; //Only these are valid.

//Now assign to a NSCharacter object to use for searching and comparing later
validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars ];

//Now create an inverteds set OF the validCharSet. 
NSCharacterSet *invertedValidCharSet = [validCharSet invertedSet];

//Now scrub your input string of bad character, those characters not in the validCharSet

NSString *scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];

//By passing in the inverted invertedValidCharSet as the characters to trim out, then you are left with only characters that are in the original set. captured here in scrubbedString.

So is this how to use this feature properly, or did I miss anything?

Thanks

Steve

like image 477
SESPROUL Avatar asked Jul 13 '13 15:07

SESPROUL


2 Answers

A character set is a just that - a set of characters. When you invert a character set you get a new set that has every character except those from the original set.

In your example you start with a character set containing the 10 standard digits. When you invert the set you get a set that has every character except the 10 digits.

like image 170
rmaddy Avatar answered Nov 15 '22 13:11

rmaddy


validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars];

This creates a character set containing the 10 characters 0, 1, ..., 9.

invertedValidCharSet = [validCharSet invertedSet];

This creates the inverted character set, i.e. the set of all Unicode characters without the 10 characters from above.

scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];

This removes from the start and end of inputString all characters that are in the invertedValidCharSet. For example, if

inputString = @"abc123d€f567ghj😄"

then

scrubbedString = @"123d€f567"

Is does not, as you perhaps expect, remove all characters from the given set.

One way to achieve that is (copied from NSString - replacing characters from NSCharacterSet):

scrubbedString = [[inputString componentsSeparatedByCharactersInSet:invertedValidCharSet] componentsJoinedByString:@""]

This is probably not the most effective method, but as your question was about understanding NSCharacterSet I hope that it helps.

like image 30
Martin R Avatar answered Nov 15 '22 14:11

Martin R