Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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
Ios 如何解析NSString以在两个不同的数组中分别获取单词和运算符_Ios_Objective C - Fatal编程技术网

Ios 如何解析NSString以在两个不同的数组中分别获取单词和运算符

Ios 如何解析NSString以在两个不同的数组中分别获取单词和运算符,ios,objective-c,Ios,Objective C,我有一根绳子 NSString *formula = @"base+unit1-unit2*unit3/unit4"; 我能够使用代码获取数组中的所有单词: NSArray *myWords = [formula componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"+-*/"]]; //myWords = {

我有一根绳子

NSString *formula = @"base+unit1-unit2*unit3/unit4";
我能够使用代码获取数组中的所有单词:

NSArray *myWords = [formula componentsSeparatedByCharactersInSet:
                            [NSCharacterSet characterSetWithCharactersInString:@"+-*/"]];

//myWords = {base , unit1,unit2,unit3,unit4}
但是我在获取数组中的所有操作符时遇到了问题,比如 此myOperators={+,-,*,/}
请给出建议,谢谢使用
NSScanner
遍历字符串,提取您需要的所有部分(请参阅
scanCharactersFromSet:intoString:
scanUpCharactersFromset:intoString:
)。

使用
NSScanner
遍历字符串,提取您需要的所有部分(请参见
scanpharactersfromset:intoString:
scanputcharactersfromset:intoString:
)。

是否需要此工作? (psudocode):


你喜欢这个工作吗? (psudocode):


您可以使用NSRegularExpression来完成此操作。快速解决方案可以是:

NSString *formula = @"base+unit1-unit2*unit3/unit4";
NSRegularExpression *testExpression = [NSRegularExpression regularExpressionWithPattern:@"[*-/]"
                                                                                         options:NSRegularExpressionCaseInsensitive error:nil];

[testExpression enumerateMatchesInString:formula
                                 options:0
                                   range:NSMakeRange(0, formula.length)
                              usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                              NSLog(@"%@", [formula substringWithRange:result.range]); 
}];
您可以使用根据正则表达式找到的所有运算符在该块中构建可变数组


正则表达式只是一个简单的例子-您可以创建比它更奇特的东西:)

您可以使用NSRegularExpression来完成这一点。快速解决方案可以是:

NSString *formula = @"base+unit1-unit2*unit3/unit4";
NSRegularExpression *testExpression = [NSRegularExpression regularExpressionWithPattern:@"[*-/]"
                                                                                         options:NSRegularExpressionCaseInsensitive error:nil];

[testExpression enumerateMatchesInString:formula
                                 options:0
                                   range:NSMakeRange(0, formula.length)
                              usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                              NSLog(@"%@", [formula substringWithRange:result.range]); 
}];
您可以使用根据正则表达式找到的所有运算符在该块中构建可变数组


正则表达式只是一个简单的例子-您可以创建比它更奇特的东西:)

您可以创建一个包含除使用invertedSet的运算符之外的所有字符的字符集

NSCharacterSet *operatorCharacters = [NSCharacterSet characterSetWithCharactersInString:@"+-/*"];
NSCharacterSet *otherCharacters = [operatorCharacters invertedSet];    

NSArray *operatorComponents = [formula componentsSeparatedByCharactersInSet:
                            otherCharacters];
然后,您需要从数组的可变副本中删除任何空字符串

NSMutableArray *mutableOperators = [operatorComponents mutableCopy];
[mutableOperators removeObject:@""];

可以使用invertedSet创建包含除运算符以外的所有字符的字符集

NSCharacterSet *operatorCharacters = [NSCharacterSet characterSetWithCharactersInString:@"+-/*"];
NSCharacterSet *otherCharacters = [operatorCharacters invertedSet];    

NSArray *operatorComponents = [formula componentsSeparatedByCharactersInSet:
                            otherCharacters];
然后,您需要从数组的可变副本中删除任何空字符串

NSMutableArray *mutableOperators = [operatorComponents mutableCopy];
[mutableOperators removeObject:@""];

你在上面>?我猜你会使用某种标记器。你在上面>?我猜你会使用某种标记器。谢谢你使用scanCharactersFromSet:IntostringHanks获得了所需的结果使用scanCharactersFromSet:intoStringThat将在数组中创建大量空字符串,必须筛选以获得最终结果,因为它在每个字符出现后分离字符串。我将添加如何执行此操作,这将在数组中创建大量空字符串,必须对其进行筛选以获得最终结果,因为它在每个字符出现后分离字符串。我将添加如何执行此操作