flo
flo

Reputation: 199

How can i compare a NSString with two other NSStrings

My problem is that i have a NSString and i have to compare it with two other NSStrings.

How can i compare NSStrings like:

if(a == (b || c))

My resolution for this isn't fine yet (but works) :

NSString *a = @"myfirststring";
NSString *b = @"mysecondstring";
if([[NSString stringWithFormat:@"%s", MethodThatReturnsChar*] isEqual:a] || [[NSString stringWithFormat:@"%s",MethodThatReturnsChar*] isEqual:b])
{
}

The problem is that i have to call MethodThatReturnsChar* two times, that isn't necessary, is it?

Upvotes: 1

Views: 113

Answers (3)

Boris Prohaska
Boris Prohaska

Reputation: 912

You can wrap your "MethodThatReturnsChar*" into another NSString using

NSString* stringToCompare = [NSString stringWithCString:yourCString encoding:NSUTF8StringEncoding];

and then compare:

if([stringToCompare isEqualToString:a] || [stringToCompare isEqualToString:b])

Hope that helps.

Upvotes: 0

wattson12
wattson12

Reputation: 11174

Assign that string to a variable, and then compare it with the other strings

    NSString *a = @"myfirststring";
    NSString *b = @"mysecondstring";
    NSString *stringIAmComparing = [NSString stringWithFormat:@"%s", MethodThatReturnsChar*];
    if([stringIAmComparing isEqualToString:a] || [stringIAmComparing isEqualToString:b])
    {
    }

Upvotes: 0

user529758
user529758

Reputation:

that isn't necessary, is it?

Exactly, it's completely superfluous. That's why the C language has variables... Also, don't abuse - [NSString stringWithFormat:]. Furthermore, use isEqualToString: for comparison:

NSString *a = [NSString stringWithUTF8String:someCharPtr];
if ([a isEqualToString:b] || [a isEqualToString:c]) {
    DoStuff();
}

Upvotes: 4

Related Questions