C# String EndsWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, EndsWith() checks whether a string ends with a given suffix. It returns true or false.

You call it on the string. The suffix is required. The default is case-sensitive. Pass StringComparison when you need ignore-case rules.

Syntax

Call EndsWith on the string. Start with this line.

C#
path.EndsWith(".txt");
CallWhat it does
path.EndsWith(".txt")Returns true when path ends with .txt.
path.EndsWith(".TXT", StringComparison.OrdinalIgnoreCase)Same check, but letter case is ignored.

This only checks the end. For a match anywhere, use Contains.

Library declarations
C#
public bool EndsWith(string value);
public bool EndsWith(string value, StringComparison comparisonType);

Parameters

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

Return Value

Return ValueDescription
trueThe string ends with the suffix.
falseThe string does not end with the suffix.

C# Examples

Example 1 — Basic EndsWith() Check

Ask whether a greeting ends with "World", then print the boolean result.

C#
using System;

class Program {
    static void Main() {
        string text = "Hello, World";

        bool ends = text.EndsWith("World");

        Console.WriteLine($"Ends with \"World\"? {ends}");

        if (ends) {
            Console.WriteLine("Suffix matched.");
        }
    }
}

How It Works

  1. Call on the string. text.EndsWith("World") checks only the trailing characters.
  2. Compare the suffix. Default rules are ordinal and case-sensitive.
  3. Get a bool. ends is true because the string ends with "World".
  4. Use in an if. Clear yes/no logic for suffix validation.

Example 2 — Case-Sensitive vs Ignore Case

File extensions often vary in casing. Default EndsWith misses .PDF when you search for .pdf.

C#
using System;

class Program {
    static void Main() {
        string file = "report.PDF";

        bool sensitive = file.EndsWith(".pdf");
        bool ignore = file.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase);

        Console.WriteLine($"Case-sensitive: {sensitive}");
        Console.WriteLine($"Ignore case:    {ignore}");
    }
}

How It Works

  1. Default is case-sensitive. ".PDF" ≠ ".pdf" → false.
  2. Pass StringComparison. OrdinalIgnoreCase folds case without culture-specific letter rules.
  3. Extension matches. Ignore-case returns true.
  4. Best practice. Prefer ordinal ignore-case for paths and file types—fast and stable across machines.

Example 3 — Filter Files by Extension

Keep only list entries that end with .cs—a common pattern for simple file filters.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var files = new List<string> {
            "Program.cs",
            "readme.md",
            "Helpers.CS",
            "data.json"
        };

        foreach (string file in files) {
            if (file.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) {
                Console.WriteLine(file);
            }
        }
    }
}

How It Works

  1. Loop the list. Each file is a candidate name.
  2. Test the suffix. Ignore-case so .CS matches .cs.
  3. Print matches only. Markdown and JSON names are skipped.
  4. Need a prefix? Switch to StartsWith for schemes like "http".

Frequently Asked Questions

EndsWith() checks whether a string ends with a given suffix and returns true or false.
value is required. comparisonType is optional for case and culture rules.
true when the suffix matches the end, false otherwise.
Yes by default. Pass StringComparison.OrdinalIgnoreCase to ignore case.
EndsWith() only checks the end. Contains() looks anywhere in the string.

Next: Equals()

Learn how to compare two strings for equality.

Equals() 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