1

I have a string like "00Welcome", "0000To", "0STACKOVERFLOW", "NOW".

Given a NSString, how can i remove zeros from prefix.

I thought initially to go with CharacterAIndex, but looks like not a good idea.

I have just gone through the below link:

Most efficient way to iterate over all the chars in an NSString

Community
  • 1
  • 1
Whoami
  • 13,930
  • 19
  • 84
  • 140
  • Iteration ??.You can perform iteration. if you get some char != '0' you stop iterating else you rewriting this char to null – vishnuvarthan Aug 07 '14 at 11:35

3 Answers3

3

If the zeroes are rare:

while ([myString hasPrefix:@"0"])
    myString = [myString subStringFromIndex:1];
gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 1
    This could potentially pollute the autorelease pool, depending on the amount of input and the amount of 0s. – dreamlax Aug 07 '14 at 11:27
3

This will remove all 0s from both then beginning and the end using stringByTrimmingCharactersInSet::

NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"0"];

NSString *input = @"00Welcome";    
NSString *output = [input stringByTrimmingCharactersInSet:charsToTrim];

Using regular expressions:

NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"^0+"];

NSString *input = @"00Welcome";    
NSString *output = [re stringByReplacingMatchesInString:input
                                                options:0
                                                  range:NSMakeRange(0, [input length])
                                           withTemplate:@""];
dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Thanks for the reply, will it remove only prefix, not suffix? – Whoami Aug 07 '14 at 11:24
  • 1
    This will remove from the end too. If you want to remove only from the beginning I will post new code, but it won't be as simple. – dreamlax Aug 07 '14 at 11:25
  • @Whoami: Added a regular expression method which will remove 0s from just the beginning. gnasher729's is shorter and suitable for small input but may cause problems if you are parsing a lot of text. The regular expression method requires compiling only one regular expression object which you can reuse for all of your input. – dreamlax Aug 07 '14 at 11:34
  • Yup. Thanks for your valuable Input. You rock always!!. :) – Whoami Aug 07 '14 at 11:55
  • May i ask can u give bit inside on why gnasher729's solution leads to autorelease pool pollute, so that i can understand? – Whoami Aug 07 '14 at 12:03
  • @Whoami: For gnasher729's solution, it creates new strings each iteration of the loop. If there are 5 zeroes at the beginning of the input, then gnasher729's solution creates 6 strings, that will all exist in memory at once. E.g., `00000Test`, `0000Test`, `000Test`, `00Test`, `0Test` and `Test`. This isn't a big problem if you are only processing a small number of strings, but if you are processing thousands of strings or the strings you are processing are very large, then it may be using memory very inefficiently. – dreamlax Aug 07 '14 at 12:28
3
NSString *str =@"000034234247236000049327428900000";
NSRange range = [str rangeOfString:@"^0*" options:NSRegularExpressionSearch];
str= [str stringByReplacingCharactersInRange:range withString:@""];

Try this alternative use of regular expressions given by @dreamlax

vishnuvarthan
  • 492
  • 1
  • 6
  • 23