Is String.Contains faster than String.IndexOf?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In software development, performance optimization is a crucial aspect, especially when dealing with large-scale applications. One common question that arises in this context is whether `String.Contains()` is faster than `String.IndexOf()` in C#. Both methods are used to search for substrings within a string, but they have distinct purposes and potentially different performance characteristics. This article delves into the technicalities to understand which method might be faster and under what circumstances.
Overview of `String.Contains()` and `String.IndexOf()`
Before comparing these two methods, let’s understand their functionalities:
- `String.Contains(String)`: Checks if a specified substring occurs within a string. It returns a boolean value (`true` or `false`) indicating the presence of the substring.
- `String.IndexOf(String)`: Searches for the first occurrence of a specified substring and returns the zero-based index of its position. If the substring is not found, it returns `-1`.
Both methods are part of the `System` namespace in C#, designed to facilitate string operations efficiently.
Technical Comparison
1. Functionality
- `String.Contains()`: It is a straightforward method that simply checks for the presence of a substring. Internally, it is implemented using `IndexOf()`, which is a more complex method.
- `String.IndexOf()`: It performs a more detailed search to find the exact position of the substring. This additional operation—returning the index instead of just presence—might generally make it slightly less efficient compared to `Contains()`, depending on the use case.
2. Performance Considerations
Performance between `Contains()` and `IndexOf()` often depends on the specific use case, particularly regarding what operations are needed once a substring is found:
- Use Case Perspective:
- If the goal is merely to check for presence, `Contains()` is usually preferable.
- If you need to know the position of the substring for further processing, `IndexOf()` is necessary.
- Internal Implementation:
- Since `Contains()` calls `IndexOf()` internally, the overhead differs primarily in what each method returns. Therefore, `Contains()` can be marginally faster when you only require a boolean outcome, due to fewer operations after locating the substring.
3. Example Scenario
To clarify the differences, consider the following C# code snippets:
- `Contains` returns `true`, indicating the substring "fox" exists within the sentence.
- `IndexOf` returns `16`, which is the starting position of "fox" within the string.
Related reading
- Is taking logs to vectorize repeated multiplication the right approach?
- Is the call to operator 'delete' synchronous?
- Is the execution time of this unique string function reduced from the naive On2 approach?
- Is the primary key automatically indexed in MySQL?
- Is Task.Delay non blocking?
- Is Task.Factory.StartNew guaranteed to use another thread than the calling thread?
- Is the runtime of BFS and DFS on a binary tree ON?
- Is the Scala 2.8 collections library a case of the longest suicide note in history?

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.