开发者

Cocoa - Trim all leading whitespace from NSString

开发者 https://www.devze.com 2023-01-06 14:24 出处:网络
(have searched, but not been able to find a simple solution to this one either here, or in Cocoa docs)开发者_Go百科

(have searched, but not been able to find a simple solution to this one either here, or in Cocoa docs)开发者_Go百科

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.


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


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.


This code is taking blanks.

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

NSLog(@"%@",trimmedText);


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];
}


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


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消