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

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

picciano
  • 22,341
  • 9
  • 69
  • 82
Raju
  • 3,459
  • 12
  • 37
  • 30

6 Answers6

283

You could use:

NSString *stringWithoutSpaces = [myString 
   stringByReplacingOccurrencesOfString:@" " withString:@""];
Mundi
  • 79,884
  • 17
  • 117
  • 140
Tom Jefferys
  • 13,090
  • 2
  • 35
  • 36
79

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:@""];
Jim Dovey
  • 11,166
  • 2
  • 33
  • 40
  • 5
    It is more useful for general usage. – mxg Jan 06 '11 at 13:01
  • i like it! I want to remove all special char in a phone number and keep only digits. – karim Mar 15 '11 at 16:23
  • much easier: yourString = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; – Daniel May 07 '13 at 16:50
  • 3
    Trimming only removes matching characters when they occur at the beginning and end of the string, so ` A B C D E F ` would become `A B C D E F`. – Jim Dovey May 15 '13 at 16:50
11

Taken from NSString

stringByReplacingOccurrencesOfString:withString:

Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

Parameters

target

The string to replace.

replacement

The string with which to replace target.

Return Value

A new string in which all occurrences of target in the receiver are replaced by replacement.

visakh7
  • 26,380
  • 8
  • 55
  • 69
10

All above will works fine. But the right method is this:

yourString = [yourString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

It will work like a TRIM method. It will remove all front and back spaces.

Thanks

Mitesh Khatri
  • 3,935
  • 4
  • 44
  • 67
  • 8
    Nope, it only removes spaces from the start and end of the string, not spaces in between other characters. – Jim Dovey May 15 '13 at 16:51
  • @JimDovey I already mention this thing in my comment. #It will work like a TRIM method. It will remove all front and back spaces.# – Mitesh Khatri May 18 '13 at 18:29
  • 2
    @MiteshKhatri You do, but it still doesn't solve the original problem, which is to remove all whitespace from within the middle of the string, not just leading & trailing whitespace. – Jim Dovey May 21 '13 at 17:22
  • @JimDovey for remove all spaces use below code: MyStr = [MyStr stringByReplacingOccurrencesOfString:@" " withString:@""]; – Mitesh Khatri Jul 26 '16 at 07:37
  • 1
    That'll still only remove ASCII space characters; things like zero-width spaces, Unicode non-breaking spaces, newlines, form feeds, tabs, vertical tabs, and so on will still remain in the string. Hence the existence of `[NSCharacterSet whitespaceCharacterSet]` and `[NSCharacterSet whitespaceAndNewlineCharacterSet]`. – Jim Dovey Nov 29 '16 at 00:03
6

if the string is mutable, then you can transform it in place using this form:

[string replaceOccurrencesOfString:@" "
                        withString:@""
                           options:0
                             range:NSMakeRange(0, string.length)];

this is also useful if you would like the result to be a mutable instance of an input string:

NSMutableString * string = [concreteString mutableCopy];
[string replaceOccurrencesOfString:@" "
                        withString:@""
                           options:0
                             range:NSMakeRange(0, string.length)];
justin
  • 104,054
  • 14
  • 179
  • 226
4

You can try this

- (NSString *)stripRemoveSpaceFrom:(NSString *)str {
    while ([str rangeOfString:@"  "].location != NSNotFound) {
        str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    return str;
}

Hope this will help you out.

Kamleshwar
  • 2,967
  • 1
  • 25
  • 27