Cocoa - Trim all leading whitespace from NSString

2019-01-13 03:54发布

问题:

(have searched, but not been able to find a simple solution to this one either here, or in Cocoa docs)

Q. How can I trim all leading whitespace only from an NSString? (i.e. leaving any other whitespace intact.)

Unfortunately, for my purposes, NSString's stringByTrimmingCharactersInSet method works on both leading and trailing.

Mac OS X 10.4 compatibility needed, manual GC.

回答1:

This creates an NSString category to do what you need. With this, you can call NSString *newString = [mystring stringByTrimmingLeadingWhitespace]; to get a copy minus leading whitespace. (Code is untested, may require some minor 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


回答2:

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 the trailing whitespaces only you can use @"\\s*$" instead.



回答3:

This code is taking blanks.

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

NSLog(@"%@",trimmedText);



回答4:

Here is a very efficient (uses CoreFoundation) way of doing it (Taken from kissxml):

- (NSString *)trimWhitespace {
    NSMutableString *mStr = [self mutableCopy];
    CFStringTrimWhitespace((CFMutableStringRef)mStr);

    NSString *result = [mStr copy];

    [mStr release];
    return [result autorelease];
}


回答5:

 NSString *myText = @"       foo    ";    
 NSString *trimmedText = [myText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
 NSLog(@"old = [%@], trimmed = [%@]", myText, trimmedText);


回答6:

Here's what I would do, and it doesn't involve 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.



回答7:

I didn't really have much time to test this, and I'm not sure if 10.4 contains the UTF8String method for NSString, but here's how I'd 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

It may not be the most efficient way of doing this but it should work.



回答8:

str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];