9

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

Community
  • 1
  • 1
SESPROUL
  • 241
  • 3
  • 8
  • 1
    Keep in mind that `stringByTrimmingCharactersInSet:` only removes characters from the ends of the string, not anything in the middle. – rmaddy Jul 13 '13 at 16:07
  • Great to know thanks!Is there a similar function for the middle of function that you are aware.? – SESPROUL Jul 13 '13 at 16:15

2 Answers2

9

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.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • 2
    Thanks for the clarification. Too many times these posts miss out this most basic description of the funcionality. Please vote up my question if possible. I am trying to get to a point where I can post pictures to my questions :) – SESPROUL Jul 13 '13 at 16:17
4
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.

Community
  • 1
  • 1
Martin R
  • 488,667
  • 78
  • 1,132
  • 1,248