Removing leading zeroes from a string
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);
Related videos on Youtube
mrugen munshi
Updated on May 29, 2020Comments
-
mrugen munshi almost 4 years
I have a string
0023525631
but what I want is the string23525631
-- without the leading zeroes. How do I do this?-
lnafziger over 11 yearsFor a comparison of the various ways of doing this (and sample code for each one) see this answer: stackoverflow.com/a/13355703/937822
-
-
vodkhang over 13 yearswhy 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 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 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 over 13 years@vodkhand: i added for you other solution
-
mrugen munshi over 13 yearsConverting the String(00123456525) into int really did the tridck for me thanks a lot ...
-
jer almost 12 yearsThis solution is not very efficient. NSScanner is very expensive. NSNumberFormatter excels in this area.
-
lnafziger over 11 yearsBelieve 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 over 11 yearsActually, NSScanner is much faster than NSNumberFormatter in this case.
-
Joshua Nozzi over 11 yearsWorks fine drawing hundreds of labels in a split second for a full-view render-to-PDF...
-
lnafziger over 11 yearsAbsolutely, and I even said that in my comment. :-)
-
lnafziger over 11 yearsYes, 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 over 11 yearsNeat. Thanks. ( stackoverflow.com/questions/13354933/… )
-
lnafziger over 11 yearsSure 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é over 10 yearsI prefer this solution, because it doesn't assume the original string can be safely converted to an integer.
-
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 over 8 yearsI'm currently working with big numbers of >1000 digits and this answer scales very well for such implementations.
-
Toad almost 8 yearsthis doesn't work as intended. It overflows for big numbers
-
Abdurrahman Mubeen Ali about 6 yearsThis would also remove the zero in 0.9!