简体   繁体   English

如何使用带有NSCharacterSet的AND或OR逻辑检查字符串

[英]How to check a string using AND or OR logic with NSCharacterSet

I am using Zxing library to scan the Barcodes. 我正在使用Zxing库扫描条形码。 The result is stored in an NSString. 结果存储在NSString中。 Here I am looking into two cases: 在这里,我研究两种情况:

Case:'semicolon' : If the result string contains semicolon character....then store it in Semicolon Array 案例:'semicolon' :如果结果字符串包含分号字符...。然后将其存储在分号数组中

myWords_semicolon = [_myString componentsSeparatedByCharactersInSet:
                            [NSCharacterSet characterSetWithCharactersInString:@";,;;"]
                            ];
       //here myWords_semicolon is a NSArray

Case: 'pipe' : If the result string contains pipe character...then store it in a pipe array. 案例:'pipe' :如果结果字符串包含管道字符...则将其存储在管道数组中。

myWords_pipe = [_myString componentsSeparatedByCharactersInSet:
                    [NSCharacterSet characterSetWithCharactersInString:@"|,||"]
           ];

What I tried to do is, if the result string contains semicolon......go to case :'semicolon' ......if the result contains pipe go to case: 'pipe'. 我想做的是,如果结果字符串包含分号……转到case:'semicolon'......如果结果包含pipe,则转到case:'pipe'。 I used this to do that but couldn't get the right solution. 我用它来做到这一点,但无法获得正确的解决方案。

if ([_myString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@";,;;"]].location != NSNotFound) {
        NSLog(@"This string doesnt contain semicolon characters");
        myWords=myWords_pipe;

    }

    if ([_myString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"|,||"]].location != NSNotFound) {
        NSLog(@"This string doesnt contain pipe characters");


        myWords=myWords_semicolon;
    }

Here in this case....only semicolon is case is working,even though I scan pipe case ,the scanner is unable to recognize the pipe case.. Is there any other way to use && or || 在这种情况下...。即使我扫描了管道箱,也只有分号起作用了,但扫描仪无法识别管道箱。还有其他使用&&或||的方法吗? logic here? 逻辑在这里?

The problem with your code is that both sets contain commas, and at the same time both strings with pipes and strings with semicolons contain commas. 您的代码的问题在于两个集合都包含逗号,同时带管道的字符串和带分号的字符串都包含逗号。 Therefore, only the assignment from the last of the two if s is going to have an effect, because both if s will "fire". 因此,只有两个if s中的最后一个赋值才会起作用,因为两个if s都将“触发”。

You should be able to fix this by removing commas and duplicate pipes from your sets. 您应该能够通过删除集中的逗号和重复的管道来解决此问题。 Moreover, you should be able to simplify it further by using rangeOfString: method instead of rangeOfCharacterFromSet: 而且,您应该能够通过使用rangeOfString:方法而不是rangeOfCharacterFromSet:来进一步简化它rangeOfCharacterFromSet:

if ([_myString rangeOfString:@";"].location != NSNotFound) {
    NSLog(@"This string contains a semicolon");
    myWords=myWords_semicolon;
}
if ([_myString rangeOfString:@"|"].location != NSNotFound) {
    NSLog(@"This string contains a pipe");
    myWords=myWords_pipe;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM