1

Code Snippet:

    NSString *tempStr = self.consumerNumber.text;        

    if ([tempStr hasPrefix:@"0"] && [tempStr length] > 1) {
        tempStr = [tempStr substringFromIndex:1];

        [self.consumerNumbers addObject:tempStr];>           
    }

I tried those things and removing only one zero. how to remove more then one zero

Output :001600240321

Expected result :1600240321

Any help really appreciated

Thanks in advance !!!!!

Nothing
  • 27
  • 1
  • 4

5 Answers5

6

Try to use this one

NSString *stringWithZeroes = @"001600240321";

NSString *cleanedString = [stringWithZeroes stringByReplacingOccurrencesOfString:@"^0+" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, stringWithZeroes.length)];

 NSLog(@"Clean String %@",cleanedString);

Clean String 1600240321

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
4

convert string to int value and re-assign that value to string,

NSString *cleanString = [NSString stringWithFormat:@"%d", [string intValue]];

o/p:-1600240321

Balu
  • 8,470
  • 2
  • 24
  • 41
0

You can add a recursive function that is called until the string begin by something else than a 0 :

 -(NSString*)removeZerosFromString:(NSString *)anyString
{
    if ([anyString hasPrefix:@"0"] && [anyString length] > 1)
    {
        return [self removeZerosFromString:[anyString substringFromIndex:1]];
    }
    else
        return anyString;
}

so you just call in your case :

NSString *tempStr = [self removeZerosFromString:@"000903123981000"];
TheTiger
  • 13,264
  • 3
  • 57
  • 82
zbMax
  • 2,756
  • 3
  • 21
  • 42
0
NSString *str = @"001600240321";
NSString *newStr = [@([str integerValue]) stringValue];

If the NSString contains numbers only. Other wise use this:

-(NSString *)stringByRemovingStartingZeros:(NSString *)string
{
    NSString *newString = string;
    NSInteger count = 0;

    for(int i=0; i<[string length]; i++)
    {
        if([[NSString stringWithFormat:@"%c",[string characterAtIndex:i]] isEqualToString:@"0"])
        {
            newString = [newString stringByReplacingCharactersInRange:NSMakeRange(i-count, 1) withString:@""];
            count++;
        }
        else
        {
            break;
        }
    }
    return newString;
}

Simply call this method:-

NSString *stringWithZeroes = @"0000000016909tthghfghf";
NSLog(@"%@", [self stringByRemovingStartingZeros:stringWithZeroes]);

OutPut: 16909tthghfghf

TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • what if str = 00123asdf ?? – Lithu T.V Jul 02 '13 at 10:10
  • In this case down voted answer would also work. :) @LithuT.V I answered according to question not according to future requirement. – TheTiger Jul 02 '13 at 10:11
  • Which Question?there is no place saying he needs only for this and it is actually a loop in code being executed ,means it can be **dynamic** .So it is **Wrong**,That is why the answers are downvoted – Lithu T.V Jul 02 '13 at 10:15
  • @LithuT.V - See updated answer. Well We can answered what we see and never can read the minds... and If the question is cleared it is always better to answer. There can me many methods to do so. – TheTiger Jul 02 '13 at 10:31
  • @LithuT.V - See **zbMax** answer too. :) – TheTiger Jul 02 '13 at 11:04
-1

Try the `stringByReplacingOccurrencesOfString´ methode like this:

NSString *new = [old stringByReplacingOccurrencesOfString: @"0" withString:@""];

SORRY: This doesn't help you due to more "0" in the middle part of your string!

JFS
  • 2,992
  • 3
  • 37
  • 48