Next The example program shows that Contains is case-sensitive when called with the default arguments.
Info In this program we call Contains 3 times. The string "abcdef" is tested with Contains.
Detail To test for strings in a case-insensitive way, try passing OrdinalIgnoreCase to the Contains() method.
static void Main()
string value = "abcdef";
Console.WriteLine("DOES NOT CONTAIN xyz");
if (value.Contains("ABC", StringComparison.OrdinalIgnoreCase))
Console.WriteLine("CONTAINS ABC ignore case");
DOES NOT CONTAIN xyz
CONTAINS ABC ignore case
Internals. Contains calls IndexOf. When IndexOf returns -1, the string was not found. When Contains cannot find the string, it returns false. Contains offers no performance advantage.
Ordinal. This refers to a position number. When used with strings, it means that the characters are treated as numbers, not symbols.
And With StringComparison.Ordinal, all language characters are treated the same—regardless of the system locale.
Performance. Ordinal comparisons on strings are much faster than culture-dependent comparisons. This makes sense because it is easier for the system to compare numbers than symbols.
Summary. The simplest method to perform a task is often the best one. Contains is a simplified version of IndexOf. It allows you to easily check whether a string is contained in another.