I have a string 0023525631
but what I want is the string 23525631
without the leading zeroes.
How do I do this; I am confused about it.
I have a string 0023525631
but what I want is the string 23525631
without the leading zeroes.
How do I do this; I am confused about it.
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.
You can use:
temp = @"0023525631";
temp = [temp stringByReplacingOccurrencesOfString:@"0" withString:@""];
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);
This is exactly the kind of thing NSNumberFormatter is made for (and it's way better at it). Why reinvent the wheel?