mrugen munshi
mrugen munshi

Reputation: 3607

Removing leading zeroes from a string

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

Upvotes: 19

Views: 18178

Answers (9)

Albert Renshaw
Albert Renshaw

Reputation: 17882

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

Upvotes: 1

neoneye
neoneye

Reputation: 52171

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)
    )
}

Upvotes: 4

Balu
Balu

Reputation: 8460

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

Upvotes: 25

Peter Hosey
Peter Hosey

Reputation: 96323

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.

Upvotes: 0

Jayprakash Dubey
Jayprakash Dubey

Reputation: 36447

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];

Upvotes: 0

Saad
Saad

Reputation: 8947

You can use this:

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

Upvotes: 8

Svisstack
Svisstack

Reputation: 16616

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.

Upvotes: 11

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

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

Upvotes: 8

Will Harris
Will Harris

Reputation: 21695

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);

Upvotes: 7

Related Questions