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

- 63,694
- 13
- 151
- 195

- 3,497
- 9
- 36
- 50
-
For a comparison of the various ways of doing this (and sample code for each one) see this answer: http://stackoverflow.com/a/13355703/937822 – lnafziger Nov 13 '12 at 05:17
9 Answers
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

- 8,470
- 2
- 24
- 41
-
5I prefer this solution, because it doesn't assume the original string can be safely converted to an integer. – René Oct 10 '13 at 13:36
-
1I'm currently working with big numbers of >1000 digits and this answer scales very well for such implementations. – Bamsworld Aug 25 '15 at 02:55
-
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.

- 16,203
- 6
- 66
- 100
-
2why 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? – vodkhang Sep 07 '10 at 10:50
-
@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 Sep 07 '10 at 11:27
-
@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 Sep 07 '10 at 11:28
-
-
Converting the String(00123456525) into int really did the tridck for me thanks a lot ... – mrugen munshi Sep 07 '10 at 12:59
You can use this:
NSString *testStr = @"001234056";
testStr = [NSString stringWithFormat:@"%d",[testStr intValue];

- 18,373
- 5
- 39
- 42

- 8,857
- 2
- 41
- 51
This is exactly the kind of thing NSNumberFormatter is made for (and it's way better at it). Why reinvent the wheel?

- 60,946
- 14
- 140
- 135
-
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 Nov 13 '12 at 05:16
-
Works fine drawing hundreds of labels in a split second for a full-view render-to-PDF... – Joshua Nozzi Nov 13 '12 at 19:16
-
-
1Yes, 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. – lnafziger Nov 13 '12 at 19:57
-
Neat. Thanks. ( http://stackoverflow.com/questions/13354933/nsstring-remove-leading-0s-so-00001234-becomes-1234/13355703#13355703 ) – Joshua Nozzi Nov 13 '12 at 20:22
-
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. :) – lnafziger Nov 13 '12 at 20:39
-
@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... – Joshua Nozzi Jul 08 '15 at 18:23
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);

- 21,597
- 12
- 64
- 64
-
This solution is not very efficient. NSScanner is very expensive. NSNumberFormatter excels in this area. – jer Jul 11 '12 at 12:04
-
2Actually, NSScanner is *much* faster than NSNumberFormatter in this case. – lnafziger Nov 13 '12 at 05:16
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)
)
}

- 50,398
- 25
- 166
- 151
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

- 1
- 1

- 17,282
- 18
- 107
- 195
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];

- 35,723
- 18
- 170
- 177
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.

- 95,783
- 15
- 211
- 370