How to Check Whether a String Contains Text with string.Contains in C#
recipe.Contains("sugar") returns True when the text sugar appears anywhere in recipe, and False when it doesn't. Upper and lower case count, unless you pass StringComparison.OrdinalIgnoreCase.
Microsoft's reference: Returns a value indicating whether a specified substring occurs within this string. A substring is any run of characters inside the string, such as a word or part of one. The remarks say how it compares: This method performs an ordinal (case-sensitive and culture-insensitive) comparison. Case-sensitive is why sugar and Sugar count as different text.
A second form takes comparison rules: Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules. Microsoft's guide to searching strings says when you need it: To accept user input or to ignore case for display text, pass a StringComparison value such as StringComparison.CurrentCultureIgnoreCase or StringComparison.OrdinalIgnoreCase.
Syntax
bool <found> = <text>.Contains(<search>);
bool <found> = <text>.Contains(<search>, <comparison>);
bool <found> = <text>.Contains(<character>);
| Part | What it is |
|---|---|
<text> | The string to search in. It must not be null |
<search> | The text to look for, as a string. It must not be null either |
<comparison> | Optional: a StringComparison value with the comparison rules, such as StringComparison.OrdinalIgnoreCase to ignore upper and lower case |
<character> | One character to look for, as a char in single quotes, such as ',' |
<found> | true when the text appears anywhere in <text>, otherwise false |
The Example
A console app called StringContains searches one line of a recipe: for a word that's in it, a word that isn't, part of a word, a word with a capital letter, with and without ignoring case, and last for a comma.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n StringContains
cd StringContains
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/StringContains/StringContains.csproj:
Determining projects to restore...
Restored /Users/you/source/StringContains/StringContains.csproj (in 46 ms).
Restore succeeded.
dotnet new console -n StringContains creates a folder named StringContains with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or StringContains.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the recipe line and two searches:
// A line from a recipe.
string recipe = "Mix the flour, sugar and butter, then add two eggs.";
Console.WriteLine(recipe);
// Contains looks for the text anywhere in the string.
Console.WriteLine($"Contains 'sugar': {recipe.Contains("sugar")}");
Console.WriteLine($"Contains 'milk': {recipe.Contains("milk")}");
recipe.Contains("sugar") returns true when sugar appears anywhere in recipe. The single quotes around the words are only part of the printed text.
Then part of a word:
// It finds part of a word too: "egg" is inside "eggs".
Console.WriteLine($"Contains 'egg': {recipe.Contains("egg")}");
Contains doesn't know about words. It looks for the letters e, g and g in a row, wherever they are.
Then upper and lower case:
// Upper and lower case count, unless you pass StringComparison.OrdinalIgnoreCase.
Console.WriteLine($"Contains 'Sugar': {recipe.Contains("Sugar")}");
Console.WriteLine($"Contains 'Sugar', ignoring case: {recipe.Contains("Sugar", StringComparison.OrdinalIgnoreCase)}");
StringComparison.OrdinalIgnoreCase is the second argument that makes Contains ignore upper and lower case. Microsoft's guide to comparing strings describes it as: Performs a case-insensitive ordinal comparison.
Last, one character:
// A char looks for one character.
Console.WriteLine($"Contains ',': {recipe.Contains(',')}");
',' in single quotes is a char, one character, so this calls the form that Returns a value indicating whether a specified character occurs within this string.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Mix the flour, sugar and butter, then add two eggs.
Contains 'sugar': True
Contains 'milk': False
Contains 'egg': True
Contains 'Sugar': False
Contains 'Sugar', ignoring case: True
Contains ',': True

The recipe contains sugar but not milk. It contains egg too, inside eggs, so Contains alone can't tell you that a whole word is there. Sugar with a capital S gave False, because the recipe has sugar in lower case, and True once StringComparison.OrdinalIgnoreCase was passed. The comma was found as a single character.
The Complete File

// A line from a recipe.
string recipe = "Mix the flour, sugar and butter, then add two eggs.";
Console.WriteLine(recipe);
// Contains looks for the text anywhere in the string.
Console.WriteLine($"Contains 'sugar': {recipe.Contains("sugar")}");
Console.WriteLine($"Contains 'milk': {recipe.Contains("milk")}");
// It finds part of a word too: "egg" is inside "eggs".
Console.WriteLine($"Contains 'egg': {recipe.Contains("egg")}");
// Upper and lower case count, unless you pass StringComparison.OrdinalIgnoreCase.
Console.WriteLine($"Contains 'Sugar': {recipe.Contains("Sugar")}");
Console.WriteLine($"Contains 'Sugar', ignoring case: {recipe.Contains("Sugar", StringComparison.OrdinalIgnoreCase)}");
// A char looks for one character.
Console.WriteLine($"Contains ',': {recipe.Contains(',')}");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling Contains on a string that is null | With string? note = null, note.Contains("sugar") warned warning CS8602: Dereference of a possibly null reference. and then stopped with Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. | Check for null first: note is not null && note.Contains("sugar") printed Note contains 'sugar': False, because && doesn't call Contains when note is not null is false |
| Searching for an empty string | With string search = "";, recipe.Contains(search) printed Contains '': True, as the reference says: true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false. An empty search box then matches every string | Check that the search text has something in it: search.Length > 0 && recipe.Contains(search) printed Search has text and is found: False |
Passing StringComparer.OrdinalIgnoreCase | recipe.Contains("Sugar", StringComparer.OrdinalIgnoreCase) didn't build: error CS1503: Argument 1: cannot convert from 'string' to 'char' and error CS1503: Argument 2: cannot convert from 'System.StringComparer' to 'System.StringComparison' | Pass a StringComparison, the type the error names: recipe.Contains("Sugar", StringComparison.OrdinalIgnoreCase), as the example does |
FAQ
How do I find where the text is?
Call IndexOf. Microsoft's guide to searching strings: IndexOf returns the zero-based index of the first occurrence of a substring (or character), and LastIndexOf returns the index of the last occurrence. Both return -1 when the search text isn't present. In the recipe, recipe.IndexOf("sugar") printed IndexOf 'sugar': 15, because counting starts at 0, and recipe.IndexOf("milk") printed IndexOf 'milk': -1.
How do I check only the start or the end of a string?
Use StartsWith or EndsWith, which take the same StringComparison values. With StringComparison.Ordinal, recipe.StartsWith("Mix", StringComparison.Ordinal) printed StartsWith 'Mix': True, recipe.StartsWith("sugar", StringComparison.Ordinal) printed StartsWith 'sugar': False, because sugar is in the middle, and recipe.EndsWith("eggs.", StringComparison.Ordinal) printed EndsWith 'eggs.': True.
Conclusion
text.Contains(search) returns true when the search text appears anywhere in the string, even inside a word, and it counts upper and lower case. Pass StringComparison.OrdinalIgnoreCase to ignore case, check for null and empty text before you search, and use IndexOf when you need the position.
Additional Resources
String.Contains— every form, its return value and how it compares.- Search strings in C# —
Contains,StartsWith,EndsWithandIndexOf. - Best practices for comparing strings in .NET — what each
StringComparisonvalue does. StringComparison— the values you can pass toContains.
