88

I want to search a specific string in the array of strings in objective c. Can somebody help me in this regard?

Dheeraj Gupta
  • 372
  • 5
  • 20
IDev
  • 1,221
  • 1
  • 11
  • 25
  • Is it your homework? I think that is easy to do. Why just looping and comparing? – vodkhang May 10 '10 at 11:30
  • na, its not homework, i needed best algorithm to search a specific string which cost not much resources in iphone, thats why i put the question – IDev May 11 '10 at 07:03
  • possible duplicate of [Objective-C Search NSArray for String?](http://stackoverflow.com/questions/3588344/objective-c-search-nsarray-for-string) – nielsbot Dec 31 '13 at 09:54

3 Answers3

197
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 :-)

TMin
  • 2,280
  • 1
  • 25
  • 34
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • lets say i have an array containing NSArray *myArr = [[NSArray alloc] initWithObjects:@"test1", @"test3",@"test3", nil]; i wanted to search "tes" lets say then i wanted a wild card stuf to work, that i could not understand, so i put this question, but thanks for the reply, appreciated – IDev May 11 '10 at 07:06
  • You either loop through and test each value, or look into NSPredicate which provides pretty comprehensive searching for collection objects - almost as powerful as SQL where clauses. http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Predicates/predicates.html#//apple_ref/doc/uid/TP40001789 – JeremyP May 11 '10 at 08:32
46

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

Rashid
  • 829
  • 7
  • 6
  • this works fine for small array but if u have very big array (more then 30k records) can u have any idea – amit gupta May 23 '15 at 04:41
  • Then keep the array sorted and use a binary search (bsearch) for locating and for inserting new elements. NSArray/NSMutableArray have methods for that. – spstanley Aug 06 '17 at 23:44
4
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];
Rahul K Rajan
  • 776
  • 10
  • 19
  • While this code snippet may answer the question it is better to include an explanation of how it answers the question in order that this answer is useful to future visitors of the site – RobV Sep 11 '14 at 09:19
  • This piece of code works fine to find the word in a array of words. – Rahul K Rajan Nov 04 '14 at 06:18
  • I want to do this with initial text. I mean search text needs to be matched from starting not from any where. Please help – Gautam Shrivastav Mar 26 '20 at 16:37