89

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

3
  • Is it your homework? I think that is easy to do. Why just looping and comparing? Commented May 10, 2010 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 Commented May 11, 2010 at 7:03
  • possible duplicate of Objective-C Search NSArray for String? Commented Dec 31, 2013 at 9:54

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
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. developer.apple.com/mac/library/documentation/cocoa/Conceptual/…
48

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

2 Comments

this works fine for small array but if u have very big array (more then 30k records) can u have any idea
Then keep the array sorted and use a binary search (bsearch) for locating and for inserting new elements. NSArray/NSMutableArray have methods for that.
5
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];

3 Comments

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
This piece of code works fine to find the word in a array of words.
I want to do this with initial text. I mean search text needs to be matched from starting not from any where. Please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.