Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString search whole text for another string

I would like to search for an NSString in another NSString, such that the result is found even if the second one does not start with the first one, for example:

eg: I have a search string "st". I look in the following records to see if any of the below contains this search string, all of them should return a good result, because all of them have "st".

Restaurant

stable

Kirsten

At the moment I am doing the following:

NSComparisonResult result = [selectedString compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];

This works only for "stable" in the above example, because it starts with "st" and fails for the other 2. How can I modify this search so that it returns ok for all the 3?

Thanks!!!

like image 312
user542584 Avatar asked Mar 02 '26 23:03

user542584


1 Answers

Why not google first?

String contains string in objective-c

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
  NSLog(@"string does not contain bla");
} else {
  NSLog(@"string contains bla!");
}
like image 183
d.lebedev Avatar answered Mar 04 '26 14:03

d.lebedev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!