C# String Contains: Usage, Case-Insensitive Checks, and Performance
Learn how to use C# String.Contains, handle case sensitivity with StringComparison, and choose between ordinal and culture-sensitive comparisons.

The Contains method on string is the simplest way to check whether a substring exists within a larger string in C#. It returns true if the substring is found, and false otherwise. The default string.Contains(string) overload performs an ordinal, case-sensitive comparison that is independent of culture. Knowing this behavior is important: many developers expect natural-language substring matching, but Contains does not apply linguistic casing rules unless you ask it to.
Basic Syntax and Behavior of String.Contains
The most direct usage is to call Contains on a string instance and pass the substring you are looking for.
string text = "The quick brown fox jumps over the lazy dog";
bool hasFox = text.Contains("fox"); // true
bool hasCat = text.Contains("cat"); // falseBecause the default comparison is case-sensitive, text.Contains("Fox") returns false even though the same letters appear in the string.
Case Sensitivity and StringComparison
For case-insensitive checks, use the overload that accepts StringComparison.
string text = "The quick brown fox";
bool hasFox = text.Contains("Fox", StringComparison.OrdinalIgnoreCase); // trueThe StringComparison enumeration controls how the search compares characters:
| Value | Behavior |
|---|---|
Ordinal | Case-sensitive, culture-insensitive, compares UTF-16 code units |
OrdinalIgnoreCase | Case-insensitive, culture-insensitive, compares code units after case folding |
CurrentCulture | Case-sensitive, uses the current culture's linguistic rules |
CurrentCultureIgnoreCase | Case-insensitive, uses the current culture's linguistic rules |
InvariantCulture | Case-sensitive, uses the invariant culture's rules |
InvariantCultureIgnoreCase | Case-insensitive, uses the invariant culture's rules |
For most substring searches in code, OrdinalIgnoreCase is the best default for case-insensitive matching: it is fast, predictable, and does not depend on the user's locale. Culture-sensitive comparisons are only needed when language-specific casing rules matter, such as for user-facing text in a localized application.
Case-Insensitive Contains in Older .NET
The Contains(string, StringComparison) overload is available in .NET Core 2.0 and later, and in .NET Standard 2.1. If you are targeting an older framework, use IndexOf with a StringComparison and check whether the result is non-negative.
// Modern .NET
bool contains = text.Contains("Fox", StringComparison.OrdinalIgnoreCase);
// Fallback for .NET Framework and older .NET Standard
bool containsLegacy = text.IndexOf("Fox", StringComparison.OrdinalIgnoreCase) >= 0;IndexOf is also the right choice when you need the position of the match, not just a boolean.
Performance Considerations
Ordinal and OrdinalIgnoreCase are the cheapest comparisons because they operate directly on UTF-16 code units. Culture-sensitive comparisons can perform additional linguistic casing and collation work, so they can be slower and their exact behavior depends on the runtime and locale.
For fixed tokens such as URL parameters, configuration keys, HTTP header names, or JSON property names, OrdinalIgnoreCase is almost always the correct choice. It keeps matching culture-independent and avoids surprising differences between environments.
Alternatives: IndexOf, Regex, StartsWith, and EndsWith
Contains is convenient when you only need a boolean answer. IndexOf is the natural choice when you also need the match position:
int index = text.IndexOf("fox", StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
// Substring found at position index
}Regular expressions handle pattern matching, such as phone numbers or email addresses, but they are more expensive than a simple substring search. StartsWith and EndsWith provide similar overloads with StringComparison when you only need to inspect the beginning or end of a string.
Handling Null and Empty Strings
Contains throws ArgumentNullException if the search substring is null. An empty search substring is always considered present, so text.Contains("") returns true.
string text = "hello";
text.Contains(null); // throws ArgumentNullException
text.Contains(""); // returns trueIf text itself can be null, check it before calling Contains, for example with string.IsNullOrEmpty.
Common Pitfalls and Edge Cases
A common mistake is to assume Contains is culture-sensitive by default. The default overload is ordinal and case-sensitive; culture-sensitive behavior only happens when you pass a culture-aware StringComparison value.
Another pitfall is searching for text such as the Turkish i and I. In Turkish, the uppercase of i is İ and the lowercase of I is ı, so culture-sensitive casing rules can produce results that differ from English. For technical identifiers, OrdinalIgnoreCase avoids these locale-dependent surprises.
Finally, remember that ordinal comparison works on UTF-16 code units, not user-perceived characters. A search for the precomposed character é will not match the sequence e plus a combining acute accent unless the string is normalized first. If you must handle decomposed text, normalize both strings (for example with string.Normalize()) before calling Contains.
For most application code, Contains with StringComparison.OrdinalIgnoreCase is a good balance of simplicity, performance, and predictable behavior.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.