NSString by removing the initial zeros?

前端 未结 5 1956
误落风尘
误落风尘 2020-12-16 17:53

How can I remove leading zeros from an NSString?

e.g. I have:

NSString *myString;

with values such as @\"0002060\"

5条回答
  •  天涯浪人
    2020-12-16 18:40

    In addition to adali's answer, you can do the following if you're worried about the string being too long (i.e. greater than 9 characters):

    NSString *str = @"000200001111111";
    NSString *strippedStr = [NSString stringWithFormat:@"%lld", [temp longLongValue]];
    

    This will give you the result: 200001111111

    Otherwise, [NSString stringWithFormat:@"%d", [temp intValue]] will probably return 2147483647 because of overflow.

提交回复
热议问题