Remove all whitespaces from NSString Remove all whitespaces from NSString objective-c objective-c

Remove all whitespaces from NSString


stringByTrimmingCharactersInSet only removes characters from the beginning and the end of the string, not the ones in the middle.

1) If you need to remove only a given character (say the space character) from your string, use:

[yourString stringByReplacingOccurrencesOfString:@" " withString:@""]

2) If you really need to remove a set of characters (namely not only the space character, but any whitespace character like space, tab, unbreakable space, etc), you could split your string using the whitespaceCharacterSet then joining the words again in one string:

NSArray* words = [yourString componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];NSString* nospacestring = [words componentsJoinedByString:@""];

Note that this last solution has the advantage of handling every whitespace character and not only spaces, but is a bit less efficient that the stringByReplacingOccurrencesOfString:withString:. So if you really only need to remove the space character and are sure you won't have any other whitespace character than the plain space char, use the first method.


I prefer using regex like this:

NSString *myString = @"this is a test";NSString *myNewString = [myString stringByReplacingOccurrencesOfString:@"\\s"                                     withString:@""                                        options:NSRegularExpressionSearch                                          range:NSMakeRange(0, [myStringlength])]; //myNewString will be @"thisisatest"

You can make yourself a category on NSString to make life even easier:

- (NSString *) removeAllWhitespace{    return [self stringByReplacingOccurrencesOfString:@"\\s" withString:@""                                              options:NSRegularExpressionSearch                                                range:NSMakeRange(0, [self length])];

}

Here is a unit test method on it too:

- (void) testRemoveAllWhitespace{    NSString *testResult = nil;    NSArray *testStringsArray        = @[@""                                         ,@"    "                                         ,@"  basicTest    "                                         ,@"  another Test \n"                                         ,@"a b c d e f g"                                         ,@"\n\tA\t\t \t \nB    \f  C \t  ,d,\ve   F\r\r\r"                                         ,@"  landscape, portrait,     ,,,up_side-down   ;asdf;  lkjfasdf0qi4jr0213 ua;;;;af!@@##$$ %^^ & *  * ()+  +   "                                         ];    NSArray *expectedResultsArray   = @[@""                                        ,@""                                        ,@"basicTest"                                        ,@"anotherTest"                                        ,@"abcdefg"                                        ,@"ABC,d,eF"                                        ,@"landscape,portrait,,,,up_side-down;asdf;lkjfasdf0qi4jr0213ua;;;;af!@@##$$%^^&**()++"                                        ];    for (int i=0; i < [testStringsArray count]; i++)    {        testResult = [testStringsArray[i] removeAllWhitespace];        STAssertTrue([testResult isEqualToString:expectedResultsArray[i]], @"Expected: \"%@\" to become: \"%@\", but result was \"%@\"",                     testStringsArray[i], expectedResultsArray[i], testResult);    }}


Easy task using stringByReplacingOccurrencesOfString

NSString *search = [searchbar.text stringByReplacingOccurrencesOfString:@" " withString:@""];