Removing leading zeroes from a string

送分小仙女□ 提交于 2019-11-27 15:28:20

You can convert string to integer and integer to string if you are number.

Or

Declarate pointer to this string and set this pointer to a start of your string, then iterate your string and add to this pointer number of zeros on the start of string. Then you have pointer to string who starting before zeros, you must use pointer to obitain string without zeros, if you use string you can get string with zeros.

Or

You can reverse string, and iterate it from reverse, if you get some char != '0' you stop iterating else you rewriting this char to null. After it you can reverse string to get back your integer in string without leading zeros.

Use Reguler Expression like this,

 NSString *str =@"000034234247236000049327428900000";
    NSRange range = [str rangeOfString:@"^0*" options:NSRegularExpressionSearch];
    str= [str stringByReplacingCharactersInRange:range withString:@""];
    NSLog(@"str %@",str);

O/P:-str 34234247236000049327428900000

This is exactly the kind of thing NSNumberFormatter is made for (and it's way better at it). Why reinvent the wheel?

Saad

You can use this:

NSString *testStr = @"001234056";
testStr = [NSString stringWithFormat:@"%d",[testStr intValue];

I'm assuming here that you only want to remove the leading zeros. I.E. @"*00*1234*0*56" becomes @"1234*0*56", not @"123457". To do that, I'd use an NSScanner.

// String to strip
NSString *test = @"001234056";

// Skip leading zeros
NSScanner *scanner = [NSScanner scannerWithString:test];
NSCharacterSet *zeros = [NSCharacterSet
                            characterSetWithCharactersInString:@"0"];
[scanner scanCharactersFromSet:zeros intoString:NULL];

// Get the rest of the string and log it
NSString *result = [test substringFromIndex:[scanner scanLocation]];
NSLog(@"%@ reduced to %@", test, result);

Without regex

func trimLeadingZeroes(input: String) -> String {
    var result = ""
    for character in input.characters {
        if result.isEmpty && character == "0" { continue }
        result.append(character)
    }
    return result
}

With regex

func trimLeadingZeroes(input: String) -> String {
    return input.stringByReplacingOccurrencesOfString(
        "^0+(?!$)",
        withString: "",
        options: .RegularExpressionSearch,
        range: Range(start: input.startIndex, end: input.endIndex)
    )
}

Better way is to write a function that can be used anytime.

-(NSString *)trimZero:(NSString*)inputString {

NSScanner *scanner = [NSScanner scannerWithString:inputString];
NSCharacterSet *zeros = [NSCharacterSet
                         characterSetWithCharactersInString:@"0"];
[scanner scanCharactersFromSet:zeros intoString:NULL];

return [inputString substringFromIndex:[scanner scanLocation]];
}

Usage :

NSString *newString = [self trimZero:yourString];

Here's a C-string-based implementation.

char *cstr = [string UTF8String];
while (*cstr == '0')
    ++cstr;
return [NSString stringWithUTF8String:cstr];

Of course, this won't also skip leading whitespace, whereas NSScanner (by default) will.

When in doubt, iterate

No need to get as complex as other answers.

Something as simple as this shall suffice.

while ([string hasPrefix:@"0"]) {
    string = [string substringFromIndex:1];
}

Notes:

•If your string's number is something like 0.12 you will be left with .12. If undesired, then simply check if hasPrefix:@"." and concatenate with a prepending @"0".

•If your string's number is just 0 you will be left with nothing. If undesired, then simply check if isEqualToString:@"" and set to @"0"

•If speed is really really important to you, you could iterate through the characters (scan) and break; upon reaching a non-zero value (at index n), then you'd simply substringFromIndex:n

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