Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.Contains(), StartsWith() and EndsWith()

Reference for C# string Contains, StartsWith and EndsWith: yes/no checks for substrings, prefixes and suffixes, with StringComparison and null rules.

These three methods answer yes/no questions about the contents of a string. Contains asks whether a substring or character appears anywhere, StartsWith whether the string begins with a given prefix, and EndsWith whether it ends with a given suffix. They return bool.

Pick them over IndexOf(...) >= 0 when you only need the answer; the code is clearer and equally fast. The default comparison differs between the overloads, so read the notes below before using them with text that is not plain English.

C#
string file = "report.pdf";
Console.WriteLine(file.EndsWith(".pdf"));     // True
Console.WriteLine(file.Contains("port"));     // True

Try it

C# The three checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Empty strings and null

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public bool Contains(string value)
public bool Contains(string value, StringComparison comparisonType)
public bool Contains(char value)
public bool Contains(char value, StringComparison comparisonType)

public bool StartsWith(string value)
public bool StartsWith(string value, StringComparison comparisonType)
public bool StartsWith(char value)

public bool EndsWith(string value)
public bool EndsWith(string value, StringComparison comparisonType)
public bool EndsWith(char value)
Name Description
value The substring, prefix or suffix to look for.
comparisonType How to compare: Ordinal, OrdinalIgnoreCase, CurrentCulture, InvariantCulture and ignore-case variants.

Returns: true if found. An empty value always gives true.

Errors: ArgumentNullException when value is null. Calling on a null string throws NullReferenceException.

Usage notes

  • Contains is ordinal; StartsWith and EndsWith (string overloads) are culture-sensitive by default. That mismatch is a classic bug source. Culture rules (Turkish dotted and dotless i, for example) can change what matches. Always pass an explicit StringComparison for identifiers, file names, headers and URLs; this site's runner uses the invariant culture.
  • Case-insensitive checks. Use StringComparison.OrdinalIgnoreCase; avoid s.ToLower().Contains(...), which allocates and may mishandle some letters.
  • Char overloads (Contains(char), StartsWith(char), EndsWith(char)) are ordinal and faster than one-character strings.
  • Empty value always matches, so guard user-supplied search text with string.IsNullOrEmpty if "empty matches everything" is wrong for you.
  • Null-safe checks. name?.StartsWith("A") == true avoids both the exception and the nullable bool problem in an if.
  • Many values. Any over an array works for a handful; for large sets use a HashSet<string> for exact matches or SearchValues for characters and substrings.
  • Collections. Contains on a List<T> or array (via LINQ) tests membership of an element, not a substring. For a string it tests the substring.