给出以下字符串:
s1 = '000001234'
s2 = '123400'
如何剥离拖尾和前导零?
在python中,我可以这样做:
s1 = s1.lstrip('0') # 1234
s2 = s2.rstrip('0') # 1234
s1, s2 = s1.strip('0'), s2.strip('0') # 1234, 1234
如果给出上面的两个字符串,我如何在Objective-C中执行等效操作?
答案 0 :(得分:1)
更简单,更清洁的解决方案:
NSString *s1 = @"0001234";
NSString *s2 = @"123400000";
while ([[s1 substringToIndex:1] isEqualToString:@"0"])
s1 = [s1 substringFromIndex:1];
while ([[s2 substringFromIndex:[s2 length]-1] isEqualToString:@"0"])
s2 = [s2 substringToIndex:[s2 length]-1];
NSLog(@"s1: %@", s1);
NSLog(@"s2: %@", s2);
答案 1 :(得分:1)
有各种可能的解决方案,这里有一个使用正则表达式。
(NSScanner
可以替代)。
NSString *s1 = @"000120340";
NSString *t1 = [s1 stringByReplacingOccurrencesOfString:@"^0*" withString:@""
options:NSRegularExpressionSearch range:NSMakeRange(0, [s1 length])];
NSLog(@"%@", t1); // "120340"
NSString *s2 = @"012034000";
NSString *t2 = [s2 stringByReplacingOccurrencesOfString:@"0*$" withString:@""
options:NSRegularExpressionSearch range:NSMakeRange(0, [s2 length])];
NSLog(@"%@", t2); // "012034"
同时从字符串的两个末尾删除零 被称为“修剪”,可以用
完成NSString *s3 = @"00012034000";
NSCharacterSet *zeroCharset = [NSCharacterSet characterSetWithCharactersInString:@"0"];
NSString *t3 = [s3 stringByTrimmingCharactersInSet:zeroCharset];
NSLog(@"%@", t3); // "12034"
答案 2 :(得分:0)
有一个简单的解决方案:数字本身没有前导零(但导致尾随),所以要获得前导零,我们只需要将它们转换为数字对象然后再转换为字符串。这将删除它们。但这并不适用于尾随零。
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
NSString *leftStripped = [f stringFromNumber:[f numberFromString:self]];
但我们可以使它工作,我们只是反转字符串,而不是创建一些字符串,从中创建一个字符串并再次反转。
首先剥离左边的零
NSString *leftStripped = [f stringFromNumber:[f numberFromString:aNumberString]];
而不是反转
NSMutableString *reversedLeftStripped = [@"" mutableCopy];
[leftStripped enumerateSubstringsInRange:NSMakeRange(0,[leftStripped length])
options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reversedLeftStripped appendString:substring];
}];
创建一个数字对象和一个来自数字
的字符串NSString *rightLeftStrippedReversed = [f stringFromNumber:[f numberFromString:reversedLeftStripped]];
并将其反转
NSMutableString *rightLeftStripped = [@"" mutableCopy];
[rightLeftStrippedReversed enumerateSubstringsInRange:NSMakeRange(0, [rightLeftStrippedReversed length]) options:NSStringEnumerationReverse| NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[rightLeftStripped appendString:substring];
}];
我会使用一个类别:
@interface NSString (Stripping)
-(NSString *)stringByStrippingLeadingAndTrailingZeros;
@end
@implementation NSString (Stripping)
-(NSString *)stringByStrippingLeadingAndTrailingZeros
{
static NSNumberFormatter *f;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
f = [[NSNumberFormatter alloc] init];
});
NSString *leftStripped = [f stringFromNumber:[f numberFromString:self]];
NSMutableString *reversedLeftStripped = [@"" mutableCopy];
[leftStripped enumerateSubstringsInRange:NSMakeRange(0,[leftStripped length])
options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reversedLeftStripped appendString:substring];
}];
NSString *rightLeftStrippedReversed = [f stringFromNumber:[f numberFromString:reversedLeftStripped]];
NSMutableString *rightLeftStripped = [@"" mutableCopy];
[rightLeftStrippedReversed enumerateSubstringsInRange:NSMakeRange(0, [rightLeftStrippedReversed length]) options:NSStringEnumerationReverse| NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[rightLeftStripped appendString:substring];
}];
return rightLeftStripped;
}
@end
所以你可以轻松打电话
NSString *s1 = @"0001234";
NSString *s2 = @"123400000";
for (NSString *string in @[s1, s2]) {
NSLog(@"%@", [string stringByStrippingLeadingAndTrailingZeros]);
}
在生产代码中,我会创建一个辅助方法来反转字符串。