Remove characters from NSString?

Viewed 144376
NSString *myString = @"A B C D E F G";

I want to remove the spaces, so the new string would be "ABCDEFG".

6 Answers

You could use:

NSString *stringWithoutSpaces = [myString 
   stringByReplacingOccurrencesOfString:@" " withString:@""];

If you want to support more than one space at a time, or support any whitespace, you can do this:

NSString* noSpaces =
    [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
                           componentsJoinedByString:@""];
Related