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?
KennyTM has already noted (in a comment) that you can use
-[NSString rangeOfString:]instead of-[NSString rangeOfCharacterFromSet]. I don’t mean to steal his helpful answer, but I wanted to point out that you can wrap this up in a category onNSStringto make it easier to use:And then you can use it like so: