Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Parse containsString make insensitive

I wonder if someone would be able to help. I am trying to search a Parse class for a term using SearchBar. The containsString however is case sensitive and i would like it to be case insensitive. Please see code below;

-(void)filterResults:(NSString *)searchTerm {

[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName: @"Firefacts"];
[query whereKeyExists:@"Number"];
[query whereKey:@"Number" containsString:searchTerm];
NSArray *results = [query findObjects];
[self.searchResults addObjectsFromArray:results];
}

Any help would be greatly appreciated.

like image 656
user3611162 Avatar asked May 07 '14 08:05

user3611162


2 Answers

Use Regular Expression (?i) instead ! credit goes to https://stackoverflow.com/a/9655203/2398886 , https://stackoverflow.com/a/9655186/2398886 and http://www.regular-expressions.info/modifiers.html

So replace

[query whereKey:@"Number" containsString:searchTerm];

with

[query whereKey:@"Number" matchesRegex:[NSString stringWithFormat:@"(?i)%@",searchTerm]];

or you can also use

[query whereKey:@"Number" matchesRegex:searchTerm modifiers:@"i"];

Your final code should be :

PFQuery *query = [PFQuery queryWithClassName: @"Firefacts"];
[query whereKeyExists:@"Number"];
[query whereKey:@"Number" matchesRegex:searchTerm modifiers:@"i"];
like image 107
Tarsem Singh Avatar answered Sep 30 '22 06:09

Tarsem Singh


For Swift use this:

query.whereKey("Number", matchesRegex: searchTerm, modifiers: "i")

reference: https://docs.parseplatform.org/ios/guide/#queries

like image 44
David Villegas Avatar answered Sep 30 '22 05:09

David Villegas