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?vodkhang– vodkhang2010-05-10 11:30:44 +00:00Commented 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 questionIDev– IDev2010-05-11 07:03:10 +00:00Commented May 11, 2010 at 7:03
-
possible duplicate of Objective-C Search NSArray for String?nielsbot– nielsbot2013-12-31 09:54:47 +00:00Commented Dec 31, 2013 at 9:54
Add a comment
|
3 Answers
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 :-)
2 Comments
IDev
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
JeremyP
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/…
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
amit gupta
this works fine for small array but if u have very big array (more then 30k records) can u have any idea
spstanley
Then keep the array sorted and use a binary search (bsearch) for locating and for inserting new elements. NSArray/NSMutableArray have methods for that.
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
RobV
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
Rahul K Rajan
This piece of code works fine to find the word in a array of words.
Gautam Shrivastav
I want to do this with initial text. I mean search text needs to be matched from starting not from any where. Please help