user523234
user523234

Reputation: 14834

manually exclude some symbols from [NSCharacterSet letterCharacterSet] invertedSet]

The code below will remove all symbols from myString. Is there a shortest way to make some exclusive symbols (say ')from being removed while still be able to use this code?

myString = [[myString componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""];

Upvotes: 1

Views: 1609

Answers (1)

Joe
Joe

Reputation: 57179

You will need to create a mutable copy and then make changes using the NSMutableCharacterSet.

NSMutableCharacterSet *mcs = [[[NSCharacterSet letterCharacterSet] invertedSet] mutableCopy];
[mcs removeCharactersInString:@"<characters you want excluded>"];

myString = [[myString componentsSeparatedByCharactersInSet:mcs] componentsJoinedByString:@""];

[mcs release];

Upvotes: 3

Related Questions