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.
string file = "report.pdf"; Console.WriteLine(file.EndsWith(".pdf")); // True Console.WriteLine(file.Contains("port")); // True
Try it
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.
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
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
Containsis ordinal;StartsWithandEndsWith(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 explicitStringComparisonfor identifiers, file names, headers and URLs; this site's runner uses the invariant culture.- Case-insensitive checks. Use
StringComparison.OrdinalIgnoreCase; avoids.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.IsNullOrEmptyif "empty matches everything" is wrong for you. - Null-safe checks.
name?.StartsWith("A") == trueavoids both the exception and the nullableboolproblem in anif. - Many values.
Anyover an array works for a handful; for large sets use aHashSet<string>for exact matches orSearchValuesfor characters and substrings. - Collections.
Containson aList<T>or array (via LINQ) tests membership of an element, not a substring. For a string it tests the substring.