Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two NSStrings named country and searchtext. I need to check whether the country contains the searchtext.

Eg: country = Iceland and searchtext = c, here the word iceland contains the character 'c'.

Thanks.

like image 217
Arun Avatar asked Dec 15 '11 09:12

Arun


2 Answers

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)
{
}
like image 165
Dennis Bliefernicht Avatar answered Nov 16 '22 02:11

Dennis Bliefernicht


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");
   }
like image 24
Sirji Avatar answered Nov 16 '22 01:11

Sirji