Skip to content

Instantly share code, notes, and snippets.

@sebk
Created November 6, 2012 20:46
Show Gist options
  • Save sebk/4027404 to your computer and use it in GitHub Desktop.
Save sebk/4027404 to your computer and use it in GitHub Desktop.
NSString removeLeadingZeros
@interface NSString (Zeros)
-(NSString*)removeLeadingZeros;
-(NSString*)addLeadingZerosTillLength:(int)length;
@end
@implementation NSString (Zeros)
-(NSString*)removeLeadingZeros {
return [self stringByReplacingOccurrencesOfString:@"^0+" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
}
-(NSString*)addLeadingZerosTillLength:(int)length {
NSMutableString *mutableString = [NSMutableString stringWithString:[self copy]];
for (int currentLength = mutableString.length; currentLength <= length-1; currentLength++) {
[mutableString insertString:@"0" atIndex:0];
}
return mutableString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment