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.