解析 NSString 以查找和递增数值
Parsing an NSString to find and increment numeric values
我有一个字符串值。
NSString *getAllData = @"0,testing,u,4,u";
现在我想递增字符串中的所有数值,例如@"1,testing,u,5,u"
如何做到这一点?
这是你的答案...
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *getAllData = @"0,testing,u,4,u";
NSMutableArray *resultArray = [NSMutableArray array];
[[getAllData componentsSeparatedByString:@","] enumerateObjectsUsingBlock:^(NSString *subString, NSUInteger idx, BOOL *stop) {
if([self isNumeric:subString])
{
[resultArray addObject:[NSString stringWithFormat:@"%d", [subString intValue] + 1]];
}
else [resultArray addObject:subString];
}];
NSString *finalOutput = [resultArray componentsJoinedByString:@","];
NSLog(@"%@", finalOutput);
}
- (BOOL)isNumeric:(NSString *)aString
{
NSString *expression = [NSString stringWithFormat:@"^[0-9]*$"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:aString options:0 range:NSMakeRange(0, [aString length])];
return (numberOfMatches != 0);
}
您可以使用NSScanner
class 查找字符串中的数字,并将它们转换为整数。 NSMutableString
class 可用于累加输出字符串。
- (NSString *)incrementValuesInString:(NSString *)input
{
NSScanner *scanner = [NSScanner scannerWithString:input];
[scanner setCharactersToBeSkipped:nil];
NSMutableString *result = [NSMutableString string];
while ( ![scanner isAtEnd] )
{
// copy characters to the result string until a digit is found
NSString *temp;
if ( [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&temp] )
[result appendString:temp];
// scan the number and increment it
if ( ![scanner isAtEnd] )
{
int value;
if ( [scanner scanInt:&value] )
[result appendFormat:@"%d", value + 1];
}
}
return( [result copy] );
}
我有一个字符串值。
NSString *getAllData = @"0,testing,u,4,u";
现在我想递增字符串中的所有数值,例如@"1,testing,u,5,u"
如何做到这一点?
这是你的答案...
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *getAllData = @"0,testing,u,4,u";
NSMutableArray *resultArray = [NSMutableArray array];
[[getAllData componentsSeparatedByString:@","] enumerateObjectsUsingBlock:^(NSString *subString, NSUInteger idx, BOOL *stop) {
if([self isNumeric:subString])
{
[resultArray addObject:[NSString stringWithFormat:@"%d", [subString intValue] + 1]];
}
else [resultArray addObject:subString];
}];
NSString *finalOutput = [resultArray componentsJoinedByString:@","];
NSLog(@"%@", finalOutput);
}
- (BOOL)isNumeric:(NSString *)aString
{
NSString *expression = [NSString stringWithFormat:@"^[0-9]*$"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:aString options:0 range:NSMakeRange(0, [aString length])];
return (numberOfMatches != 0);
}
您可以使用NSScanner
class 查找字符串中的数字,并将它们转换为整数。 NSMutableString
class 可用于累加输出字符串。
- (NSString *)incrementValuesInString:(NSString *)input
{
NSScanner *scanner = [NSScanner scannerWithString:input];
[scanner setCharactersToBeSkipped:nil];
NSMutableString *result = [NSMutableString string];
while ( ![scanner isAtEnd] )
{
// copy characters to the result string until a digit is found
NSString *temp;
if ( [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&temp] )
[result appendString:temp];
// scan the number and increment it
if ( ![scanner isAtEnd] )
{
int value;
if ( [scanner scanInt:&value] )
[result appendFormat:@"%d", value + 1];
}
}
return( [result copy] );
}