C# String Contains() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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.

C#
text.Contains("fun");
CallWhat it does
text.Contains("fun")Returns true when fun appears anywhere in text.
text.Contains("FUN", StringComparison.OrdinalIgnoreCase)Same check, but letter case is ignored.

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

ParameterDescription
valueRequired. The substring to find. Do not pass null.
comparisonTypeOptional. Rules for case and culture, such as OrdinalIgnoreCase.

Return Value

Return ValueDescription
trueThe substring was found.
falseThe 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.");
        }
    }
}

How It Works

  1. Call on the string. text.Contains("fun") searches inside text.
  2. Scan for a substring. Default rules are ordinal and case-sensitive.
  3. Get a bool. found is true because "fun" appears at the end.
  4. 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}");
    }
}

How It Works

  1. Default is case-sensitive. "Fun" ≠ "fun" → false.
  2. Pass StringComparison. OrdinalIgnoreCase folds case without culture-specific letter rules.
  3. Same substring, different result. Ignore-case returns true.
  4. 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);
            }
        }
    }
}

How It Works

  1. Loop the list. Each line is a candidate string.
  2. Test with Contains. Ignore-case so "error" would also match.
  3. Print matches only. Non-matching lines are skipped.
  4. 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.

Next: Copy()

Learn how to copy a string into a new string instance.

Copy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful