Cocoa - Trim all leading spaces from NSString

(searched, but could not find a simple solution for this either here or in Cocoa docs)

Q. How can I trim all leading spaces from NSString only? (i.e. leave any other spaces int.)

Unfortunately, for my purposes, the NSString stringByTrimmingCharactersInSet method works with both the leading and the ending.

Requires compatibility with Mac OS X 10.4, GC User Guide.

+43
trim cocoa nsstring whitespace
Jul 08 2018-10-10T00:
source share
8 answers

This creates an NSString category to do what you need. You can call NSString *newString = [mystring stringByTrimmingLeadingWhitespace]; to get a copy minus leading spaces. (The code is untested, it may take a little debugging.)

 @interface NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace; @end @implementation NSString (trimLeadingWhitespace) -(NSString*)stringByTrimmingLeadingWhitespace { NSInteger i = 0; while ((i < [self length]) && [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[self characterAtIndex:i]]) { i++; } return [self substringFromIndex:i]; } @end 
+67
Jul 08 '10 at 4:16
source share

This is another solution using regular expressions (requires iOS 3.2):

 NSRange range = [string rangeOfString:@"^\\s*" options:NSRegularExpressionSearch]; NSString *result = [string stringByReplacingCharactersInRange:range withString:@""]; 

And if you want to trim trailing spaces, you can use @"\\s*$" instead.

+49
Dec 28 '10 at
source share

This code accepts spaces.

NSString *trimmedText = [strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSLog(@"%@",trimmedText);

+36
May 08 '12 at 18:57
source share

Here is a very efficient way to use CoreFoundation (taken from kissxml):

 - (NSString *)trimWhitespace { NSMutableString *mStr = [self mutableCopy]; CFStringTrimWhitespace((CFMutableStringRef)mStr); NSString *result = [mStr copy]; [mStr release]; return [result autorelease]; } 
+25
Nov 28 '11 at 9:15
source share
  NSString *myText = @" foo "; NSString *trimmedText = [myText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; NSLog(@"old = [%@], trimmed = [%@]", myText, trimmedText); 
+21
Apr 20 2018-12-12T00:
source share

Here is what I would like to do, and it’s not related to categories!

 NSString* outputString = inputString; NSRange range = [inputString rangeOfCharacterFromSet: [NSCharacterSet whitespaceCharacterSet] options:0]; if (range.location == 0) outputString = [inputString substringFromIndex: range.location + range.length]; 

This is much less code.

+3
Jul 08 '10 at 5:27
source share

I did not have much time to verify this, and I am not sure that 10.4 contains the UTF8String method for NSString, but here is how I do it:

NSString + Trimming.h

 #import <Foundation/Foundation.h> @interface NSString (Trimming) -(NSString *) stringByTrimmingWhitespaceFromFront; @end 

NSString + Trimming.m

 #import "NSString+Trimming.h" @implementation NSString (Trimming) -(NSString *) stringByTrimmingWhitespaceFromFront { const char *cStringValue = [self UTF8String]; int i; for (i = 0; cStringValue[i] != '\0' && isspace(cStringValue[i]); i++); return [self substringFromIndex:i]; } @end 

This may not be the most efficient way to do this, but it should work.

0
Jul 08 '10 at 4:17
source share
 str = [str stringByReplacingOccurrencesOfString:@" " withString:@""]; 
-5
Nov 20
source share



All Articles