NSString: remove leading 0s so '00001234' becomes '1234'? [duplicate]

青春壹個敷衍的年華 提交于 2019-11-27 07:06:08

问题


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


回答1:


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!



回答2:


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.




回答3:


This works, I've tested it. And I think this is a very good way. Less code less bug. KISS.

NSString *numStr = @"000123";
int num = numStr.intValue;
numStr = [NSString stringWithFormat:@"%d", num];


来源:https://stackoverflow.com/questions/13354933/nsstring-remove-leading-0s-so-00001234-becomes-1234

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!