Question

I'm looking for the Objective-C equivalent to the C# method TrimStart(string).

Something like:

- (NSString*)trimStart:(NSString*)inputString withTrimString:(NSString*)trimChars {
    return inputString;
}

but where

[self trimStart:@"333test1233" withTrimString:@"3"];

returns test1233

And is not just trimming all 3's from the string.

Was it helpful?

Solution 2

@implementation NSString (Trimming)

-(NSString*)trimCharactersFromStart:(NSString*)characters
{
    NSCharacterSet* set = [[NSCharacterSet characterSetWithCharactersInString:characters] invertedSet];
    NSRange         range = [self rangeOfCharacterFromSet:set];

    if(range.location == NSNotFound)
        return @"";
    else if(range.location != 0)
        return [self substringFromIndex:range.location];
    else
        return self;
}

@end

Note that if you were trying to trim the characters from the start and end of the string you could just use stringByTrimmingCharactersInSet:

OTHER TIPS

I would suggest using -rangeOfCharacterFromSet with the NSAnchoredSearch option. Then use the result of this in a call to -substringFromIndex to remove the chars at the start of the string.

This should do

+ (NSString*)trimStart:(NSString*)inputString withTrimString:(NSString*)trimChars {
    NSCharacterSet* trimSet = [NSCharacterSet characterSetWithCharactersInString:trimChars];
    for(NSUInteger i = 0; i < [inputString length]; i++) {
        if (![trimSet characterIsMember:[inputString characterAtIndex:i]]) {
            return [inputString substringFromIndex:i];
        }
    }
    return @"";
}
- (NSString*)trimStart:(NSString*)inputString withTrimString:(NSString*)trimChars 
{
    NSString *buffer = inputString;
    NSInteger i = 0;
    NSCharacterSet *charSet = [NSCharacterSet charactersSetWithCharactersInString:trimChars];    

    while ((i<[buffer length])&&[charSet characterIsMember:[buffer characetrAtIndex:i])
    {
      i ++;
    }

 return [buffer substringFromIndex:i];
}

It looks to me like the closest thing in the Cocoa string libraries is the string method stringByTrimmingCharactersInSet: However, that method will trim characters from both the beginning and the end.

You might need to write your own trimStart method. It wouldn't be that hard. Something like this:

 -(NSString *)trimCharacter: (unichar) charToTrim fromStartOfString: (NSString *) string
 {
     NSUIndex index = 0;
     if ([string length] == 0)
         return nil;
     unichar thisChar = [string characterAtIndex: index];
     while (thisChar == charToTrim) do
     {
         thisChar = [string characterAtIndex: index++];
     }
     return [string substringFromIndex: index];
 }

Note that some people are suggesting using rangeOfCharacterFromSet:options:, with an options value of NSAnchoredSearch.

This does not work. The following code:

NSCharacterSet *theSet = [NSCharacterSet characterSetWithCharactersInString: @"3"];

NSString *stringToTrim = @"333test1233";

NSRange range = [stringToTrim rangeOfCharacterFromSet: theSet options: NSAnchoredSearch];

NSLog(@"Range.location = %d, range.length = %d", range.location, range.length);

Returns

Range.location = 0, range.length = 1

Which is not what you want. You want to find the range of ALL the characters to skip.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top