How to find out a specific character is present in a NSString or not? How to find out a specific character is present in a NSString or not? xcode xcode

How to find out a specific character is present in a NSString or not?


Try this:

NSRange range = [country rangeOfString:searchtext];if (range.location != NSNotFound){}

You also have the position (location) and length of your match (uninteresting in this case but might be interesting in others) in your range object. Note that searchtext must not be nil. If you are only interested in matching (and not the location) you can even condense this into

if ([country rangeOfString:searchtext].location != NSNotFound){}


NSString *st =    @"Iceland";NSString *t_st = @"c";      NSRange rang =[st rangeOfString:t_st options:NSCaseInsensitiveSearch];   if (rang.length == [t_st length])    {          NSLog(@"done");   }   else   {          NSLog(@"not done");   }


Very simple... try this

-(BOOL)doesString:(NSString *)string containCharacter:(char)character{    if ([string rangeOfString:[NSString stringWithFormat:@"%c",character]].location != NSNotFound)    {        return YES;    }    return NO;}