Search code examples
iphoneiosstringnsstringtexttrimming

How to remove starting 0's in uitextfield text in iphone sdk


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 !!!!!


Solution

  • 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