Remove Any Character Or Set Of Characters From String Using NSCharacterSet and NSMutableCharacterSet

Although the NSString class has methods such as rangeOfCharacterFromSet: and stringByReplacingOccurrencesOfString:withString:, often times you may need more flexibility than either of these two methods afford.

For example, if you need to remove all occurrences of any character in a set, NSCharacter and NSMutableCharacterSet are the way to go.

Let’s assume you wanted to remove all numbers from the string “99 bottles of beer on the wall”

NSString *str = @"99 bottles of beer on the wall";
NSLog(@"String: %@", str);
 
// Create character set with digits only
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet decimalDigitCharacterSet];
 
// Build array of components using digits as separator
NSArray *arrayOfComponents = [str componentsSeparatedByCharactersInSet:characterSet];
 
// Create string from the array components
NSString *strOutput = [arrayOfComponents componentsJoinedByString:@""];
 
NSLog(@"New string: %@", strOutput);

The output will looks as follows:

String: 99 bottles of beer on the wall
New string: bottles of beer on the wall

Let’s look at a more common example, parsing phone numbers. If you have a phone number in the format “(123)-456-7890” you can remove all the special characters by creating a character set that has those specific characters that you want to eliminate:

NSString *str = @"(123)-456-7890";
NSLog(@"String: %@", str);
 
// Create character set with specified characters
NSMutableCharacterSet *characterSet = 
  [NSMutableCharacterSet characterSetWithCharactersInString:@"()-"];
 
// Build array of components using specified characters as separtors
NSArray *arrayOfComponents = [str componentsSeparatedByCharactersInSet:characterSet];
 
// Create string from the array components
NSString *strOutput = [arrayOfComponents componentsJoinedByString:@""];
 
NSLog(@"New string: %@", strOutput);

The output is shown below:

String: (123)-456-7890
New string: 1234567890

As a final example, let’s combine a number of different character sets. To remove symbols, letters, punctuation and whitespace from a string, try this:

NSString *str = @"Price with tax: $250";
NSLog(@"String: %@", str);
 
// All the characters to remove
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet symbolCharacterSet];
[characterSet formUnionWithCharacterSet:[NSCharacterSet letterCharacterSet]];
[characterSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[characterSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
 
// Build array of components using specified characters as separtors
NSArray *arrayOfComponents = [str componentsSeparatedByCharactersInSet:characterSet];
 
// Create string from the array components
NSString *strOutput = [arrayOfComponents componentsJoinedByString:@""];
 
NSLog(@"New string: %@", strOutput);

String: Price with tax: $250
New string: 250

Okay, one more example just came to mind…instead of specifying all the characters to parse, we can do the opposite and specify only the characters we are interested to keep, this is done by inverting a character set:

NSString *str = @"Price with tax: $250";
NSLog(@"String: %@", str);
 
// All the characters to remove
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet decimalDigitCharacterSet];
 
// Invert the character set
NSCharacterSet *characterSetInverted = [characterSet invertedSet];
 
// Build array of components using specified characters as separtors
NSArray *arrayOfComponents = 
  [str componentsSeparatedByCharactersInSet:characterSetInverted];
 
// Create string from the array components
NSString *strOutput = [arrayOfComponents componentsJoinedByString:@""];
 
NSLog(@"New string: %@", strOutput);

Same output as above:

String: Price with tax: $250
New string: 250

Please feel free to post your code examples below.

  1. simple, although for really complicated stuff NSRegularExpression available in iOS 4.0+ works pretty well.

Comments are closed.