82

I have a couple of strings. Some have a whitespace in the beginning and some not. I want to check if a string begins with a whitespace and if so remove it.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Crazer
  • 837
  • 1
  • 7
  • 6
  • A little searching wouldn't hurt. Have a look at [What's the best way to trim whitespace from a string in Cocoa Touch?](http://stackoverflow.com/questions/474270/whats-the-best-way-to-trim-whitespace-from-a-string-in-cocoa-touch) – Abizern Jan 10 '11 at 10:25

4 Answers4

230

There is method for that in NSString class. Check stringByTrimmingCharactersInSet:(NSCharacterSet *)set. You should use [NSCharacterSet whitespaceCharacterSet] as parameter:

NSString *foo = @" untrimmed string ";
NSString *trimmed = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • 4
    This will also trim trailing whitespace (which may or may not be desirable. (The original question asks to just remove it from the beginning). – lnafziger May 25 '13 at 04:12
  • 2
    `stringByTrimmingCharactersInSet` only removes characters from the beginning and the end of the string, not the ones in the middle. For those who are trying to remove space in the middle of a string, use `[yourString stringByReplacingOccurrencesOfString:@" " withString:@""]`. – Arnab May 02 '17 at 08:45
79

You could use the stringByTrimmingCharactersInSet NSString method with the whitespaceAndNewlineCharacterSet NSCharacterSet as such:

NSString *testString = @"  Eek! There are leading and trailing spaces  ";
NSString *trimmedString = [testString stringByTrimmingCharactersInSet:
                             [NSCharacterSet whitespaceAndNewlineCharacterSet]];
John Parker
  • 54,048
  • 11
  • 129
  • 129
  • 4
    +1 for recommending the whitespaceAndNewlineCharacterSet rather than just the whitespaceCharacterSet. – Rik Smith-Unna Mar 22 '12 at 22:58
  • 2
    -1 to Apple for redefining whitespace to not include newlines (they should have made a new inlineWhitespaceCharacterSet instead) – AlexChaffee Apr 14 '13 at 19:13
  • This will also trim trailing whitespace (which may or may not be desirable. (The original question asks to just remove it from the beginning). – lnafziger May 25 '13 at 04:12
8

This will remove only the leading white space.

NSString *myString = @"   123   ";
NSLog(@"mystring %@, length %d",myString, myString.length);
NSRange range = [myString rangeOfString:@"^\\s*" options:NSRegularExpressionSearch];
myString = [myString stringByReplacingCharactersInRange:range withString:@""];
NSLog(@"mystring %@, length %d",myString, myString.length);

output

mystring    123   , length 9
mystring 123   , length 6
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
8

I wrote a quick macro to reduce the amount of code needed to be written.

Step 1: Edit your app's PCH file, this should be named Project-Name-Prefix.pch

#define TRIM(string) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]

Step 2: Enjoy writing way less code when you want to trim a string

NSLog(@"Output: %@ %@", TRIM(@"Hello        "), TRIM(@"World      "));

Output: Hello World
user511037
  • 147
  • 2
  • 3