简体   繁体   中英

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?

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

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])
    {
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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