Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在符号之间加入字符串的一部分?_Iphone_Ios_Cocoa - Fatal编程技术网

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现在我只是理解它!