Comparing string

Discussion in 'Mac Programming' started by rbert, Nov 15, 2009.

  1. rbert macrumors newbie

    Joined:
    Nov 15, 2009
    #1
    I have a myString that i break into an array of words.
    My goal is to determinate if the content of the myString mets the conditions:
    - if myString contains all of "0", it prints out "Failed!"
    - if myString contains all of "1", it prints out "Passed!"
    - if myString contains a mixture of "1" and "0", it prints out "Partially Passed!"

    Here is the code i try but it does not work in the while loop
    Can you please help me, thanks!

    PHP:
        NSString *myString = @"1 1 1 2 0 1 2 0 1 0";
        
        
    //break NSString *myString into an NSArray containing 
        //the individual words separated by whitespace, newlines, and punctuation
        
    NSMutableCharacterSet *separators = [NSMutableCharacterSet punctuationCharacterSet];
        [
    separators formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
    NSArray *words = [myString componentsSeparatedByCharactersInSet:separators];

        
    NSEnumerator *enumerator;
        
    enumerator = [words objectEnumerator];
        
    NSString *stringContents;
        
        
    NSString *result;

        
    //cycles through array
            //It does not work up to here:
        
    while (stringContents = [enumerator nextObject]) {
        
            if ([
    stringContents isEqualToString:@"0"]) {
    result=@"Failed!";
            }
            
            if ([
    stringContents isEqualToString:@"1"]) {
    result=@"Passed!";
            }
            
            if ([
    stringContents isEqualToString:@"0"] && [stringContents isEqualToString:@"1"]) {
    result=@"Partially Passed!";
            }
            
        }
        
        
    //print out result either failed, passed or partially passed.
        //but it does not work with the output outside the loop.
        
    NSLog(@"%@"result);
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    How about this:

    Code:
    BOOL hasZero = [myString rangeOfString:@"0"].location != NSNotFound;
    BOOL hasOne = [myString rangeOfString:@"1"].location != NSNotFound;
    
    if (hasZero && hasOne) {
        result=@"Partially Passed!";
    } else if (hasZero) {
        result=@"Failed!";
    } else if (hasOne) {
        result=@"Passed!";
    }
    
     
  3. rbert thread starter macrumors newbie

    Joined:
    Nov 15, 2009
    #3
    Thank You!

    omg :eek: it's soo short and exactly what i am looking for.
    Thank you kpua for the very elegant solution!!
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    What if the string has no zeros or ones? Should nothing be printed? Should an error be printed?

    -Lee
     
  5. rbert thread starter macrumors newbie

    Joined:
    Nov 15, 2009
    #5
    Thanks lee, i added the else condition for not defined.
    I have another question, is there a way to defined the string range for BOOL hasOne from 1 to 100 instead of using a fixed @"1", so that it match more. Something like this:
    Thank you for your help!

    PHP:
    NSString *myString = @"1 1 1 2 0 1 2 0 1 0";

    BOOL hasZero = [myString rangeOfString:@"0"].location != NSNotFound;
    //BOOL hasOne = [myString rangeOfString:@"1"].location != NSNotFound;
    BOOL hasOne = [myString rangeOfString:{1-100}].location != NSNotFound;

    if (
    hasZero && hasOne) {
        
    result=@"Partially Passed!";
    } else if (
    hasZero) {
        
    result=@"Failed!";
    } else if (
    hasOne) {
        
    result=@"Passed!";
    } else {
        
    result=@"Not defined!";
    }

    NSLog(@"%@"result); 
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    explain yourself more clearly.

    Do you only want to check if there is a 1 in the first 100 characters of myString, or do you want to see if the string representation of the numbers 1 - 100 appear? If the latter, 0 appears in 10, 20, 30, etc., so are you looking for these values as "words" in the string?

    If you're looking for words, you can do something like this:
    Code:
    NSString *myString = @"1 1 1 2 0 1 2 0 1 0";
    BOOL hasZero = NO;
    BOOL hasNonZero = NO;
    BOOL hasInvalid = NO;
    NSArray splitString = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    for(NSString singleString in splitString) {
      if([singleString isEqualToString:@"0"]) {
        hasZero = YES;
      } else {
        int intRep = [singleString intValue];
        if(intRep > 0 && intRep <= 100) {
          hasNonZero = YES;
        } else {
          hasInvalid = YES;
        }
      }
      if(hasZero && hasNonZero && hasInvalid) break; //Nothing else to see
    }
    
    if (hasZero && hasNonZero) {
        result=@"Partially Passed!";
    } else if (hasZero) {
        result=@"Failed!";
    } else if (hasNonZero) {
        result=@"Passed!";
    } else if(hasInvalid) {
        result=@"Not defined!";
    }
    
    NSLog(@"%@", result);  
    
    I haven't compiled this, so it may not work 100% as is, but the idea should be clear.

    -Lee
     
  7. rbert thread starter macrumors newbie

    Joined:
    Nov 15, 2009
    #7
    Unbelievable, you code it with pure brain without testing and very quick! :eek:
    Yes i am looking for these values as "words" in the string and your code is full working, i just admire you!
    Thank you very much lee!!!

    I have tested it and it is working:
    PHP:

        
    /* Code written by lee1210 @ macrumors.com */

        //NSString *myString = @"1 2 3 40 50 60 70"; // Out: Passed! > Correct
        //NSString *myString = @"0 0 0 0 0 0 0"; // Out: Failed! > Correct
        
    NSString *myString = @"1 2 0 40 50 60 70"// Out: Partially Passed! > Correct

        
    BOOL hasZero NO;
        
    BOOL hasNonZero NO;
        
    BOOL hasInvalid NO;
        
    NSString *result;
        
        
    //break NSString *myString into an NSArray containing 
        //the individual words separated by whitespace, newlines, and punctuation
        
    NSMutableCharacterSet *separators = [NSMutableCharacterSet punctuationCharacterSet];
        [
    separators formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
    NSArray *splitString = [myString componentsSeparatedByCharactersInSet:separators];

        
    //cycles through array
        
    for(NSString *singleString in splitString) {
            if([
    singleString isEqualToString:@"0"]) {
                
    hasZero YES;
            } else {
                
    int intRep = [singleString intValue];
                if(
    intRep && intRep <= 100) {
                    
    hasNonZero YES;
                } else {
                    
    hasInvalid YES;
                }
            }
            if(
    hasZero && hasNonZero && hasInvalid) break; //Nothing else to see
        
    }
        
        
    //print result
        
    if (hasZero && hasNonZero) {
            
    result=@"Partially Passed!";
        } else if (
    hasZero) {
            
    result=@"Failed!";
        } else if (
    hasNonZero) {
            
    result=@"Passed!";
        } else if(
    hasInvalid) {
            
    result=@"Not defined!";
        }
        
        
    NSLog(@"%@"result);
     

Share This Page