In C#, Contains() checks whether a substring appears anywhere in a string. It returns true or false.
You call it on the string you search in. The search text is required. The default is case-sensitive. Pass StringComparison when you need ignore-case rules.
Syntax
Call Contains on the string you already have. Start with this line.
Need the index? Use IndexOf. Need only the end? Use EndsWith.
Library declarations
C#
public bool Contains(string value);
public bool Contains(string value, StringComparison comparisonType);
Parameters
Parameter
Description
value
Required. The substring to find. Do not pass null.
comparisonType
Optional. Rules for case and culture, such as OrdinalIgnoreCase.
Return Value
Return Value
Description
true
The substring was found.
false
The substring was not found.
C# Examples
Example 1 — Basic Contains() Check
Ask whether a sentence includes the word "fun", then print the boolean result.
C#
using System;
class Program {
static void Main() {
string text = "C# is fun!";
bool found = text.Contains("fun");
Console.WriteLine($"Contains \"fun\"? {found}");
if (found) {
Console.WriteLine("Match found.");
}
}
}
Output
Contains "fun"? True
Match found.
How It Works
Call on the string.text.Contains("fun") searches inside text.
Scan for a substring. Default rules are ordinal and case-sensitive.
Get a bool.found is true because "fun" appears at the end.
Use in an if. Clear yes/no logic—no index math required.
Example 2 — Case-Sensitive vs Ignore Case
Default Contains misses "Fun" inside "fun". Pass OrdinalIgnoreCase to ignore letter case.
C#
using System;
class Program {
static void Main() {
string text = "C# is fun!";
bool sensitive = text.Contains("Fun");
bool ignore = text.Contains("Fun", StringComparison.OrdinalIgnoreCase);
Console.WriteLine($"Case-sensitive: {sensitive}");
Console.WriteLine($"Ignore case: {ignore}");
}
}
Output
Case-sensitive: False
Ignore case: True
How It Works
Default is case-sensitive."Fun" ≠ "fun" → false.
Pass StringComparison.OrdinalIgnoreCase folds case without culture-specific letter rules.
Same substring, different result. Ignore-case returns true.
When to use Ordinal. Prefer ordinal options for tags, codes, and protocol text—fast and stable.
Example 3 — Filter Items That Contain a Keyword
Keep only list entries that include a search term—common for simple search boxes and log filtering.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var lines = new List<string> {
"INFO: started",
"ERROR: disk full",
"WARN: slow query",
"ERROR: timeout"
};
string keyword = "ERROR";
foreach (string line in lines) {
if (line.Contains(keyword, StringComparison.OrdinalIgnoreCase)) {
Console.WriteLine(line);
}
}
}
}
Output
ERROR: disk full
ERROR: timeout
How It Works
Loop the list. Each line is a candidate string.
Test with Contains. Ignore-case so "error" would also match.
Print matches only. Non-matching lines are skipped.
Need the index later? Switch to IndexOf when you must slice around the match.
Frequently Asked Questions
Contains() checks whether a substring appears in a string and returns true or false.
value is required. comparisonType is optional for case and culture rules.
true when the substring exists, false when it does not.
Yes by default. Pass StringComparison.OrdinalIgnoreCase to ignore case.
Use Contains() for yes/no. Use IndexOf() when you need the starting index.