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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With