Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~3 min readC#
#String.Contains#StringComparison#case-insensitive#C# string performance
Illustration of a C# string being checked for a substring with case-insensitive comparison options.

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.

csharp
string text = "The quick brown fox jumps over the lazy dog"; bool hasFox = text.Contains("fox"); // true bool hasCat = text.Contains("cat"); // false

Because 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.

csharp
string text = "The quick brown fox"; bool hasFox = text.Contains("Fox", StringComparison.OrdinalIgnoreCase); // true

The StringComparison enumeration controls how the search compares characters:

ValueBehavior
OrdinalCase-sensitive, culture-insensitive, compares UTF-16 code units
OrdinalIgnoreCaseCase-insensitive, culture-insensitive, compares code units after case folding
CurrentCultureCase-sensitive, uses the current culture's linguistic rules
CurrentCultureIgnoreCaseCase-insensitive, uses the current culture's linguistic rules
InvariantCultureCase-sensitive, uses the invariant culture's rules
InvariantCultureIgnoreCaseCase-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.

csharp
// 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:

csharp
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.

csharp
string text = "hello"; text.Contains(null); // throws ArgumentNullException text.Contains(""); // returns true

If 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 tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.