问题描述
- 删除UItextField的起始0
-
代码:NSString *tempStr = self.consumerNumber.text; if ([tempStr hasPrefix:@"0"] && [tempStr length] > 1) { tempStr = [tempStr substringFromIndex:1]; [self.consumerNumbers addObject:tempStr];> }
输出:001600240321
期望输出:1600240321
谢谢!
解决方案
NSString *stringWithZeroes = @"001600240321";
NSString *cleanedString = [stringWithZeroes stringByReplacingOccurrencesOfString:@"^0+" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, stringWithZeroes.length)];
NSLog(@"Clean String %@",cleanedString);
这样就可以了。
解决方案二:
NSString *str=@"001600240321";
str=[str stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0"]]
NSLog(@"%@",str);
时间: 2024-09-25 20:51:55