PDA

View Full Version : Comparing string




rbert
Nov 15, 2009, 09:51 AM
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!


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);



kpua
Nov 15, 2009, 10:50 AM
How about this:


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!";
}

rbert
Nov 15, 2009, 11:17 AM
omg :eek: it's soo short and exactly what i am looking for.
Thank you kpua for the very elegant solution!!

lee1210
Nov 15, 2009, 11:22 AM
What if the string has no zeros or ones? Should nothing be printed? Should an error be printed?

-Lee

rbert
Nov 15, 2009, 05:11 PM
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!

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);

lee1210
Nov 15, 2009, 06:28 PM
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:

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

rbert
Nov 15, 2009, 07:14 PM
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:


/* 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 > 0 && 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);