本文介绍了NSString:删除前导0,使'00001234'变为'1234'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要要删除 NSString 中的前导0,我能想到的一种快速方法是将字符串转换为 NSNumber 然后转换回 NSString 这将给我干净的字符串,虽然我不确定它是否有效。还有其他办法吗?

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?

谢谢

推荐答案

所以只是为了好玩,我决定用各种方式来做这件事。这绝不是一次科学测试,主要是为了我的娱乐,但是你们中的一些人可能还是希望看到它。

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.

我创建了一个方法并替换了各种例程处理代表数字0到100,000的字符串,所有字符串都带有三个前导0。

I created a method and substituted the various routines to process strings representing the numbers 0 - 100,000 all with three leading 0's.

但请记住,如果你只有一个(甚至一百个)这些字符串要修剪,即使是最慢的方法也是完全可以接受的。 (使用数字格式化程序需要大约.000012039905秒,或大约1/100毫秒。)除非您确实需要处理大型文件,否则其他内容(例如代码阅读和理解的容易程度)通常更为重要。像这样的字符串。我个人最喜欢的仍然是正则表达式,因为即使没有文档,它也相对较快而且很明显你想要完成什么

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):

循环播放字符串

// 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

转换为int然后返回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

使用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

使用正则表达式

NSRange range = [string rangeOfString:@"^0*" options:NSRegularExpressionSearch];
return [string stringByReplacingCharactersInRange:range withString:@""];

// Time 1: 0.610879
// Time 2: 0.645335
// Time 3: 0.637690

使用静态数字格式化程序

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

使用数字格式化程序

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!

这篇关于NSString:删除前导0,使'00001234'变为'1234'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:52