Possible Duplicate:
Removing leading zeroes from a string
I need to remove leading 0s in an NSString
, a quick way I can think of is to convert the string to an NSNumber
then convert back to NSString
which will give me the clean string, although I'm not sure if it works. Is there any other way to do this?
Thanks
So just for fun, I decided to time the various ways of doing this. This was by no means a scientific test, done mostly for my amusement, but some of you might like to see it anyway.
I created a method and substituted the various routines to process strings representing the numbers 0 - 100,000 all with three leading 0's.
Keep in mind however, that even the slowest method is going to be perfectly acceptable if you only have one (or even one hundred) of these strings to trim. (Using the number formatter takes about .000012039905 seconds, or about 1/100th of a millisecond.) Other things such as how easy your code is to read and understand is usually more important unless you really do need to process a large file full of strings like this. My personal favorite is still the regular expression, because it is relatively fast and immediately obvious what you are trying to accomplish, even without documentation.
Here are the results (from fastest to slowest):
Looping through the string
// I tried this by looping through the utf8String and got the same times
// (actually, ever so slightly longer, probably since it had to create the c string)
//
// I did find that using `[string getCharacters:buffer range:range]` and
// iterating over the character buffer that it took another 0.01 seconds
// off the time. Hardly worth it though. :)
NSUInteger length = [string length];
for (NSUInteger i = 0; i < length; i++)
{
if ([string characterAtIndex:i] != '0')
{
return [string substringFromIndex:i];
}
}
return @"0";
// Time 1: 0.210126
// Time 2: 0.219159
// Time 3: 0.201496
Converting to an int and then back to a NSString
int num = [string intValue];
return [NSString stringWithFormat:@"%d", num];
// Time 1: 0.322206
// Time 2: 0.345259
// Time 3: 0.324954
long long num = [string longLongValue];
return [NSString stringWithFormat:@"%lld", num];
// Time 1: 0.364318
// Time 2: 0.344946
// Time 3: 0.364761
// These are only slightly slower, but you can do bigger numbers using long long
Using a NSScanner
NSScanner *scanner = [NSScanner scannerWithString:string];
NSCharacterSet *zeros = [NSCharacterSet
characterSetWithCharactersInString:@"0"];
[scanner scanCharactersFromSet:zeros intoString:NULL];
return [string substringFromIndex:[scanner scanLocation]];
// Time 1: 0.505277
// Time 2: 0.481884
// Time 3: 0.487209
Using a regular expression
NSRange range = [string rangeOfString:@"^0*" options:NSRegularExpressionSearch];
return [string stringByReplacingCharactersInRange:range withString:@""];
// Time 1: 0.610879
// Time 2: 0.645335
// Time 3: 0.637690
Using a static number formatter
static NSNumberFormatter *formatter = nil;
if (formatter == nil)
{
formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
}
NSNumber *number = [formatter numberFromString:string];
return [formatter stringFromNumber:number];
// Time 1: 1.774198
// Time 2: 1.753013
// Time 3: 1.753893
Using a number formatter
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSNumber *number = [formatter numberFromString:string];
return [formatter stringFromNumber:number];
// Time 1: 11.978336
// Time 2: 12.039905
// Time 3: 11.904984
// No wonder Apple recommends reusing number formatters!
You could use regular expressions.
NSString *numStr = @"0001234";
NSRange range = [numStr rangeOfString:@"^0*" options:NSRegularExpressionSearch];
NSString *result = [numStr stringByReplacingCharactersInRange:range withString:@""];
If you doing it a lot (as in thousands of times) you may benefit from using a compiled NSRegularExpression
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With