I am trying to develop an app that contains an array with a huge number of words. I want to create a new filtered array from it. Filtering is done based on a pattern that I have managed to create using a regular expression. For example, I should be able to filter out the word "apple" with a pattern "ap_l_" from my array with words. Can anyone help me out?
Asked
Active
Viewed 4,653 times
0
-
2What do you mean by that? You need to provide a little more information. – rdelmar Aug 22 '12 at 15:30
-
Possible Duplicate: http://stackoverflow.com/questions/110332/filtering-nsarray-into-a-new-nsarray-in-objective-c – jtomschroeder Aug 22 '12 at 15:32
4 Answers
6
Use the below code it will filter the array
-(NSMutableArray *)searchByContains:(NSString *)containsString inputArray:(NSMutableArray *)inputArray
{
NSLog(@"orginal Array count=%d",[inputArray count]);
NSString *expression=[NSString stringWithFormat:@"SELF contains '%@'",containsString];
NSLog(@"expression=%@",expression);
NSPredicate *predicate = [NSPredicate predicateWithFormat:expression];
NSMutableArray *mArrayFiltered = [[inputArray filteredArrayUsingPredicate:predicate] mutableCopy];
return mArrayFiltered;
}

Fevicks
- 223
- 1
- 10
2
Best way to filter an array is to use predicates. If you have an array of strings, for example:
NSArray *stringsArray = [NSArray arrayWithObjects:@"Joe", @"Bill", @"David", @"Jeff", nil];
you can easily filter it using filteredArrayUsingPredicate:. If, for example, you wanted to filter the above array for all instances of @"Bill", you would do it like this:
NSArray *filteredArray = [stringsArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF == 'Bill'"]];
if you want to filter OUT @"Bill", then you would do this:
filteredArray = [stringsArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != 'Bill'"]];
and so on.

JAB
- 3,165
- 16
- 29
1
if all element is string you can use .
for(NSString *str in arrayName)
{
if([str isEqualToString:@"searchString"])
{
//wirte own code here
}
}

Hardeep Singh
- 942
- 8
- 15
1
if i understood your question... try this
if([your_array containsObject: your_string]){
do something
}
hope this helps

Neo
- 2,807
- 1
- 16
- 18