String search in string array in objective c
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Objective-C provides several ways to search for a string within an NSArray of strings. containsObject: checks for exact matches, indexOfObject: returns the position, filteredArrayUsingPredicate: supports partial and case-insensitive matching with NSPredicate, and block-based enumeration gives full control over the search logic. The right method depends on whether you need exact matches, substring matches, or case-insensitive comparisons.
Exact Match with containsObject:
containsObject: uses isEqual: for comparison, which is case-sensitive for NSString.
Finding the Index with indexOfObject:
indexOfObject: returns NSNotFound if the string is not in the array.
Substring and Case-Insensitive Search with NSPredicate
Predicate modifiers: [c] = case-insensitive, [d] = diacritic-insensitive, [cd] = both.
Block-Based Enumeration
Case-Insensitive Search with rangeOfString:
For manual case-insensitive comparison:
Regex Search with NSPredicate
MATCHES uses ICU regex syntax.
Searching in NSMutableArray
All methods above work identically with NSMutableArray. Additionally, you can remove non-matching elements:
Performance Considerations
| Method | Time Complexity | Best For |
containsObject: | O(n) | Quick exact match check |
indexOfObject: | O(n) | Finding position of exact match |
filteredArrayUsingPredicate: | O(n) | Flexible pattern matching |
indexOfObjectPassingTest: | O(n) worst case | Custom logic with early exit |
NSSet containsObject: | O(1) average | Repeated lookups in large collections |
For repeated searches on a large array, convert to NSSet first:
Common Pitfalls
- Case sensitivity with
containsObject::containsObject:usesisEqual:, which is case-sensitive for strings.[@[@"Hello"] containsObject:@"hello"]returnsNO. UseNSPredicatewith[c]for case-insensitive matching. - Forgetting
NSNotFoundcheck:indexOfObject:returnsNSNotFound(which isNSIntegerMax) when the string is not found. Using the result as an index without checking causes an out-of-bounds crash. - Predicate format string injection: Building predicates with
stringWithFormat:instead ofpredicateWithFormat:can cause crashes with special characters. Always usepredicateWithFormat:with%@substitution. - Searching in nil arrays: Sending messages to
nilin Objective-C returnsnil/0/NO.[nil containsObject:@"test"]returnsNOwithout crashing, which may hide bugs where the array was unexpectedly nil. - Performance with large arrays:
filteredArrayUsingPredicate:scans the entire array every time. For repeated lookups, build anNSSetorNSDictionaryfor O(1) access.
Summary
- Use
containsObject:for simple, exact-match existence checks - Use
indexOfObject:when you need the position of the match - Use
NSPredicatewithCONTAINS[cd],BEGINSWITH[c], orMATCHESfor flexible string matching - Use block-based
indexOfObjectPassingTest:for custom search logic with early exit - Convert to
NSSetfor O(1) lookups when searching the same collection repeatedly - Always check for
NSNotFoundbefore using an index returned byindexOfObject:
Related reading
- String Tiling Algorithm
- String to Dictionary in Python
- String to unique integer hashing
- string.joinlist on object array rather than string array
- stringByAppendingPathComponent is unavailable
- stringByAppendingPathComponent is unavailable
- Stumped with functional breadth-first tree traversal in Clojure?
- Subgraph enumeration

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.