Removing leading zeroes from a string

17,445

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

You can use this:

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

Solution 5

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);
Share:
17,445

Related videos on Youtube

mrugen munshi
Author by

mrugen munshi

Updated on May 29, 2020

Comments

  • mrugen munshi
    mrugen munshi almost 4 years

    I have a string 0023525631 but what I want is the string 23525631 -- without the leading zeroes. How do I do this?

  • vodkhang
    vodkhang over 13 years
    why do you need to reverse string, I think a forward iteration will be better? You can just iterate and if you meet 0, remove, not 0, stop?
  • Svisstack
    Svisstack over 13 years
    @vodkhang: No because you can't set null at start of your string. And with reverse, lenght of string was changed (in second reverse) because second reverse get smaller iteration (depends on how many nulls was writed)
  • Svisstack
    Svisstack over 13 years
    @vodkhand: you dont must reverse your string but you must use other moved pointer to your string (moved to right depends on number of zeros)
  • Svisstack
    Svisstack over 13 years
    @vodkhand: i added for you other solution
  • mrugen munshi
    mrugen munshi over 13 years
    Converting the String(00123456525) into int really did the tridck for me thanks a lot ...
  • jer
    jer almost 12 years
    This solution is not very efficient. NSScanner is very expensive. NSNumberFormatter excels in this area.
  • lnafziger
    lnafziger over 11 years
    Believe it or not, this is the slowest of all of the suggested methods. Not that it matters for a one off (or probably even a 1000 off) but if you need an efficient way of doing this, this isn't the way to go.
  • lnafziger
    lnafziger over 11 years
    Actually, NSScanner is much faster than NSNumberFormatter in this case.
  • Joshua Nozzi
    Joshua Nozzi over 11 years
    Works fine drawing hundreds of labels in a split second for a full-view render-to-PDF...
  • lnafziger
    lnafziger over 11 years
    Absolutely, and I even said that in my comment. :-)
  • lnafziger
    lnafziger over 11 years
    Yes, take a look at the link that I posted here (on the original question) to my answer on another question here at SO if you want to see the difference between this and other methods.
  • Joshua Nozzi
    Joshua Nozzi over 11 years
  • lnafziger
    lnafziger over 11 years
    Sure thing, hopefully you find it useful. For what it's worth though, your method was the first thing that came to mind for me as well. :)
  • René
    René over 10 years
    I prefer this solution, because it doesn't assume the original string can be safely converted to an integer.
  • Joshua Nozzi
    Joshua Nozzi almost 9 years
    @JAL What would you have me do, copy and paste the entire API reference for the exact class name I mentioned and kindly linked to help readers avoid a needless search of the documentation? What if the OP was asking about "some visual control I can click that makes stuff happen"? Mention and link the NSButton API reference or mention NSButton and paste its entire API reference? There's such a thing as taking community guidelines to ridiculous extremes...
  • Bamsworld
    Bamsworld over 8 years
    I'm currently working with big numbers of >1000 digits and this answer scales very well for such implementations.
  • Toad
    Toad almost 8 years
    this doesn't work as intended. It overflows for big numbers
  • Abdurrahman Mubeen Ali
    Abdurrahman Mubeen Ali about 6 years
    This would also remove the zero in 0.9!