Compare Objective-C const char with NSString

HurkNburkS picture HurkNburkS · Feb 28, 2013 · Viewed 11.6k times · Source

I was wondering if there is a simple way to compare a const char with an NSString, or do I have to convert the const char to an NSString before doing do?

I have been looking through Apple docs but struggling to find an answer to my question.

Answer

CodaFi picture CodaFi · Feb 28, 2013

Either

NSString *str = @"string";
const char *myChar = "some string";
if (strcmp(myChar,  str.UTF8String))

or

[str isEqualToString:[NSString stringWithUTF8String:myChar]];

The core foundation route is also an option:

CFStringRef myStr = CFSTR("some chars");

bool result = CFStringCompareWithOptions(myStr, ((__bridge CFStringRef)str),CFRangeMake(0,CFStringGetLength(myStr)), kCFCompareCaseInsensitive);