Created
November 6, 2012 20:46
-
-
Save sebk/4027404 to your computer and use it in GitHub Desktop.
NSString removeLeadingZeros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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