Iphone 在符号之间加入字符串的一部分?
我希望能够把`符号后面和任何非数字字符前面的数字转换成整数 前 谢谢,Iphone 在符号之间加入字符串的一部分?,iphone,ios,cocoa,Iphone,Ios,Cocoa,我希望能够把`符号后面和任何非数字字符前面的数字转换成整数 前 谢谢, 问候。有更好的方法可以做到这一点,很快我就能想出这个 NSString *mystring = @"123(12`)"; NSString *neededString = nil; NSScanner *scanner =[NSScanner scannerWithString:mystring]; [scanner scanUpToString:@"`" intoString:&neededString]; nee
问候。有更好的方法可以做到这一点,很快我就能想出这个
NSString *mystring = @"123(12`)";
NSString *neededString = nil;
NSScanner *scanner =[NSScanner scannerWithString:mystring];
[scanner scanUpToString:@"`" intoString:&neededString];
neededString = [self reverseString:neededString];
NSLog(@"%@",[self reverseString:[NSString stringWithFormat:@"%d",[neededString intValue]]]);
要反转字符串,你可以看到有更好的方法,很快我就能想出这个
NSString *mystring = @"123(12`)";
NSString *neededString = nil;
NSScanner *scanner =[NSScanner scannerWithString:mystring];
[scanner scanUpToString:@"`" intoString:&neededString];
neededString = [self reverseString:neededString];
NSLog(@"%@",[self reverseString:[NSString stringWithFormat:@"%d",[neededString intValue]]]);
要反转可以看到的字符串,可以使用正则表达式。您可以找到以下所有事件:
NSString *mystring = @"123(12`)456+1093`";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([0-9]+)`" options:0 error:nil];
NSArray *matches = [regex matchesInString:mystring options:0 range:NSMakeRange(0, mystring.length)];
for (NSTextCheckingResult *match in matches) {
NSLog(@"%@", [mystring substringWithRange:[match rangeAtIndex:1]]);
}
// 12 and 1093
如果只需要一次引用,则将for循环替换为以下内容:
if (matches.count>0) {
NSTextCheckingResult *match = [matches objectAtIndex:0];
NSLog(@"%@", [mystring substringWithRange:[match rangeAtIndex:1]]);
}
您可以使用正则表达式。您可以找到以下所有事件:
NSString *mystring = @"123(12`)456+1093`";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([0-9]+)`" options:0 error:nil];
NSArray *matches = [regex matchesInString:mystring options:0 range:NSMakeRange(0, mystring.length)];
for (NSTextCheckingResult *match in matches) {
NSLog(@"%@", [mystring substringWithRange:[match rangeAtIndex:1]]);
}
// 12 and 1093
如果只需要一次引用,则将for循环替换为以下内容:
if (matches.count>0) {
NSTextCheckingResult *match = [matches objectAtIndex:0];
NSLog(@"%@", [mystring substringWithRange:[match rangeAtIndex:1]]);
}
工作正如预期,除非有多个:P现在我只是理解它!工作正如预期,除非有多个:P现在我只是理解它!