0

I am using Zxing library to scan the Barcodes. The result is stored in an NSString. Here I am looking into two cases:

Case:'semicolon' : If the result string contains semicolon character....then store it in Semicolon Array

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.

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'. 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?

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • 1
    What do your input strings look like? Do you understand that `[NSCharacterSet characterSetWithCharactersInString:@";,;;"]` creates a character set whose members are a semicolon `;` and a comma `,`? Doubling the semicolon at the end of the string has no effect. – jscs Sep 26 '14 at 02:27
  • Can a string contain both `;` and `|`? What should happen in that case? – Sergey Kalinichenko Sep 26 '14 at 02:34
  • I'm trying to figure out why you use `characterSetWithCharactersInString` like that. How is ";,;;" different from ";,"? – Hot Licks Sep 26 '14 at 02:34
  • Also, note that there are several different characters that look like the "pipe" character. Are you sure your source strings are using 0x7C and not some "national" character? – Hot Licks Sep 26 '14 at 02:36
  • @dasblinkenlight no, my string is sanitized to have either semicolon or pipe – Teja Nandamuri Sep 26 '14 at 02:48
  • my string input looks like this 123||sdsdsd||2323,4556||sdsd||1212| or 123;asdasd;;1212;;1212,12,12;;sdsd; – Teja Nandamuri Sep 26 '14 at 02:49
  • @HOtLicks ---- ";," looks for ; and , and ";,;;" looks for ; and , and ;;. Here i used ";,;;" to recognize two semicolons that present side by side!!! – Teja Nandamuri Sep 26 '14 at 02:51
  • No, that's not how character set works. – Hot Licks Sep 26 '14 at 02:53
  • Ummm....may be not....but i used the same line of code to differentiate strings by looking at ; , ;; and it worked!!! – Teja Nandamuri Sep 26 '14 at 02:55
  • `";,;;"` is a set of `;` and `,`. It will work to separate at semicolons so long as there are no spurious commas in the string. – Hot Licks Sep 26 '14 at 02:56
  • (And don't keep recreating your character sets. Create them once and use them where needed.) – Hot Licks Sep 26 '14 at 02:57

1 Answers1

1

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 ifs is going to have an effect, because both ifs will "fire".

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:

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;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523