I have a NsMutableArray. I filtered matched data with NSPredicate but i want that array element which is not matched.
Can anyone help me.
You can use NOT with the predicate for that
NSArray *arrValues = [NSArray arrayWithObjects:#"Hello",#"Hello One",#"Good Morning", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"NOT (SELF CONTAINS %#)",#"Hello"];
NSArray *arrFiltered = [arrValues filteredArrayUsingPredicate:predicate];
NSLog(#"%#",arrFiltered);
Output
(
"Good Morning"
)
Related
It is possible to filter an array of strings as follows:
NSArray *array = #[#"honda",#"toyota",#"ford"];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF contains[cd] %#",#"ford"];
NSArray *filtered = [array filteredArrayUsingPredicate:pred];
I want to search an array that contains arrays of two strings by the values for the first of the strings. So for:
NSArray *cars = #[#[#"honda",#"accord"],#[#"toyota",#"corolla"],#[#"ford",#"explorer"]];
I want to search the first dimension (honda, toyota, ford) for #"ford"
Is there a way to tell the predicate I want to search on only the first attribute and return matching elements of the array?
Well here is the pred you need.
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF[FIRST] contains[cd] %#", #"ford"];
NSArray *arrValues = [NSArray arrayWithObjects:#"ABCD",#"ABCE",#"CDE"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF CONTAINS[cd]%#",#"ABC"];
NSArray *arrFiltered = [arrValues filteredArrayUsingPredicate:predicate];
I am aware that arrFiltered will contain the result ABCD and ABCE.
But, I want the result as CDE.
Is there a way to find the inverse of the predicate specified. ie., !(ABC)
Try to use NOT with predicate
NSArray *arrValues = [NSArray arrayWithObjects:#"ABCD",#"ABCE",#"CDE"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"NOT (SELF CONTAINS[cd] %#"),#"ABC"];
NSArray *arrFiltered = [arrValues filteredArrayUsingPredicate:predicate];
try
[NSPredicate predicateWithFormat:#"SELF !=[c] %#",#"ABC"];
The [c] makes the equality comparison case-insensitive.
Option-2
NSArray *arrValues = [NSArray arrayWithObjects:#"ABCD",#"ABCE",#"CDE",nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"NOT (SELF CONTAINS %#)",#"ABC"];
NSArray *arrFiltered = [arrValues filteredArrayUsingPredicate:predicate];
you get output as
I have a NSMutableArray of objects with tag attributes assigned to them. I am trying to find if any of these objects have a tag assigned to them of the string value "a".
my code so far:
for (Object *object in self.array)
{
NSDictionary *attrs = [object propertyValue:#"attrs"];
NSString *tag = attrs[#"tag"];
}
Can I do something like:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"a"];
NSArray *results = [self.array filteredArrayUsingPredicate:predicate];
not sure how it works?
Try this
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"tag == %#", #"a"];
NSArray *result = [NSMutableArray arrayWithArray:[self.array filteredArrayUsingPredicate:predicate]];
Use 'contains[c]' instead of '==' if you want to perform case-insensitive search.
If you want to filter for any other property, replace 'tag' with that property name.
You will need to do something more like :
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.property_name contains[c] '%#'", #"a"];
NSArray *result = [NSMutableArray arrayWithArray:[self.array filteredArrayUsingPredicate:predicate]];
Hi I'm using NSPredicate to search objects of NSArrays against a 5 elements array, and get the result back. But is there any way to get the matched results returned by NSPredicate as well as the string (among that 5 elements array) against which match succeeded?
try like this ,
this predicate gives the array which not contains that substring.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"not SELF contains '%#'",searchString];
NSArray *array = [NSArray arrayWithObjects:#" AhfjA ", #"test1", #"best", #"AntA", nil];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate ];
NSLog(#"%#",filteredArray );
O/P:-
(
AhfjA,
best,
AntA
)
it gives the array which contains the substring
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains '%#'",searchString];
NSArray *array = [NSArray arrayWithObjects:#" AhfjA ", #"test1", #"best", #"AntA", nil];
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate ];
NSLog(#"%#",filteredArray );
O/P:-
(
test1,
)
I want to search a specific string in the array of strings in objective c. Can somebody help me in this regard?
BOOL isTheObjectThere = [myArray containsObject: #"my string"];
or if you need to know where it is
NSUInteger indexOfTheObject = [myArray indexOfObject: #"my string"];
I strongly recommend you read the documentation on NSArray. It's best to do that before posting your question :-)
You can use NSPredicate class for searching strings in array of strings.
See the below sample code.
NSMutableArray *cars = [NSMutableArray arrayWithObjects:#"Maruthi",#"Hyundai", #"Ford", #"Benz", #"BMW",#"Toyota",nil];
NSString *stringToSearch = #"i";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#",stringToSearch]; // if you need case sensitive search avoid '[c]' in the predicate
NSArray *results = [cars filteredArrayUsingPredicate:predicate];
This is the most efficient way for searching strings in array of strings
NSMutableArray *cars = [NSMutableArray arrayWithObjects:#"Max",#"Hai", #"Fine", #"Bow", #"Bomb",#"Toy",nil];
NSString *searchText = #"i";
NSArray *results = [cars filteredArrayUsingPredicate:predicate];
// if you need case sensitive search avoid '[c]' in the predicate
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:#"title contains[c] %#",
searchText];
searchResults = [cars filteredArrayUsingPredicate:resultPredicate];