I have a string 0023525631
but what I want is the string 23525631
-- without the leading zeroes. How do I do this?
问题:
回答1:
You can convert string to integer and integer to string if you are number.
Or
Declarate pointer to this string and set this pointer to a start of your string, then iterate your string and add to this pointer number of zeros on the start of string. Then you have pointer to string who starting before zeros, you must use pointer to obitain string without zeros, if you use string you can get string with zeros.
Or
You can reverse string, and iterate it from reverse, if you get some char != '0'
you stop iterating else you rewriting this char to null. After it you can reverse string to get back your integer in string without leading zeros.
回答2:
Use Reguler Expression like this,
NSString *str =@"000034234247236000049327428900000";
NSRange range = [str rangeOfString:@"^0*" options:NSRegularExpressionSearch];
str= [str stringByReplacingCharactersInRange:range withString:@""];
NSLog(@"str %@",str);
O/P:-str 34234247236000049327428900000
回答3:
This is exactly the kind of thing NSNumberFormatter is made for (and it's way better at it). Why reinvent the wheel?
回答4:
You can use this:
NSString *testStr = @"001234056";
testStr = [NSString stringWithFormat:@"%d",[testStr intValue];
回答5:
I'm assuming here that you only want to remove the leading zeros. I.E. @"*00*1234*0*56" becomes @"1234*0*56", not @"123457". To do that, I'd use an NSScanner.
// String to strip
NSString *test = @"001234056";
// Skip leading zeros
NSScanner *scanner = [NSScanner scannerWithString:test];
NSCharacterSet *zeros = [NSCharacterSet
characterSetWithCharactersInString:@"0"];
[scanner scanCharactersFromSet:zeros intoString:NULL];
// Get the rest of the string and log it
NSString *result = [test substringFromIndex:[scanner scanLocation]];
NSLog(@"%@ reduced to %@", test, result);
回答6:
Without regex
func trimLeadingZeroes(input: String) -> String {
var result = ""
for character in input.characters {
if result.isEmpty && character == "0" { continue }
result.append(character)
}
return result
}
With regex
func trimLeadingZeroes(input: String) -> String {
return input.stringByReplacingOccurrencesOfString(
"^0+(?!$)",
withString: "",
options: .RegularExpressionSearch,
range: Range(start: input.startIndex, end: input.endIndex)
)
}
回答7:
Better way is to write a function that can be used anytime.
-(NSString *)trimZero:(NSString*)inputString {
NSScanner *scanner = [NSScanner scannerWithString:inputString];
NSCharacterSet *zeros = [NSCharacterSet
characterSetWithCharactersInString:@"0"];
[scanner scanCharactersFromSet:zeros intoString:NULL];
return [inputString substringFromIndex:[scanner scanLocation]];
}
Usage :
NSString *newString = [self trimZero:yourString];
回答8:
Here's a C-string-based implementation.
char *cstr = [string UTF8String];
while (*cstr == '0')
++cstr;
return [NSString stringWithUTF8String:cstr];
Of course, this won't also skip leading whitespace, whereas NSScanner (by default) will.
回答9:
When in doubt, iterate
No need to get as complex as other answers.
Something as simple as this shall suffice.
while ([string hasPrefix:@"0"]) {
string = [string substringFromIndex:1];
}
Notes:
•If your string's number is something like 0.12
you will be left with .12
. If undesired, then simply check if hasPrefix:@"."
and concatenate with a prepending @"0".
•If your string's number is just 0
you will be left with nothing. If undesired, then simply check if isEqualToString:@""
and set to @"0"
•If speed is really really important to you, you could iterate through the characters (scan) and break;
upon reaching a non-zero value (at index n
), then you'd simply substringFromIndex:n