strchr in objective C?

Posted by Brian Postow on Stack Overflow See other posts from Stack Overflow or by Brian Postow
Published on 2010-04-01T18:47:05Z Indexed on 2010/04/01 18:53 UTC
Read the original article Hit count: 424

Filed under:
|
|

I'm trying to write the equivalent of strchr, but with NSStrings... I've currently got this:

Boolean nsstrchr(NSString* s, char c)
{
    NSString *tmps = [NSString stringWithFormat: @"%c", c];
    NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString: tmps];
    NSRange rg = [s rangeOfCharacterFromSet: cSet];
    return rg.location != NSNotFound;
}

This seems needlessly complex... Is there a way to do this (preferably, one that doesn't involve turning the NSString into a cstring which doubles the run time, or writing it myself using characterAtIndex:... Am I missing some obvious method in the NSString description?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about strings