C# Contains

String type

Like IndexOf, Contains searches strings. You want to check if one substring is contained in another. The Contains method on string in C# is ideal for this purpose, as it provides a case-sensitive ordinal method for checking string contents.

This C# program shows the Contains method on the string type. The implementation of Contains is revealed.

Example

Note

Contains is an instance method on the string type, which means you can call it on a specific string in your program. It has a bool result, which is true if the parameter is found, and false if it is not found. The next example program shows that Contains is case-sensitive. It shows how you can test the result of Contains.

Example program that uses Contains [C#]

using System;

class Program
{
    static void Main()
    {
	Test("Dot Net Perls");
	Test("dot net perls");
    }

    static void Test(string input)
    {
	Console.Write("--- ");
	Console.Write(input);
	Console.WriteLine(" ---");
	//
	// See if the string contains 'Net'
	//
	bool contains = input.Contains("Net");
	//
	// Write the result
	//
	Console.Write("Contains 'Net': ");
	Console.WriteLine(contains);
	//
	// See if the string contains 'perls' lowercase
	//
	if (input.Contains("perls"))
	{
	    Console.WriteLine("Contains 'perls'");
	}
	//
	// See if the string contains 'Dot'
	//
	if (!input.Contains("Dot"))
	{
	    Console.WriteLine("Doesn't Contain 'Dot'");
	}
    }
}

Output

--- Dot Net Perls ---
Contains 'Net': True
--- dot net perls ---
Contains 'Net': False
Contains 'perls'
Doesn't Contain 'Dot'
Main method

Description. The Main entry point in this code passes a string parameter to the Test method. In that method, the output is printed the console. The string 'Net' is first tested with Contains. It is found in the first string, but not found in the second string. This shows that Contains is case-sensitive.

Testing the result of Contains. This method checks Contains in if conditionals. It shows how you can test it for true and false. Also, you can store the result of Contains in a bool variable.

Implementation

.NET Framework information

You should be familiar with the IndexOf method on string. What you will find is that Contains is actually just a wrapper for IndexOf. When IndexOf returns -1, it means that the string was not found; when Contains cannot find the string, it returns false instead. In IL Disassembler, we can see how Contains is implemented.

IL Disassembler TutorialQuestion and answer

What does ordinal mean? The word ordinal refers to a number that indicates a position in a sequence. When used with strings, it means that the characters are treated as numbers, not symbols. With StringComparison.Ordinal, all language characters are treated the same, regardless of the system locale.

Performance. I have found that 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.

IndexOf String Examples

Summary

The C# programming language

We saw how to use the Contains method on the string type in the C# language. This is a simplified version of IndexOf. It allows you to easily check to see a string is contained in another. It is not affected by the system locale, so it is reliable on all systems.

String Type
.NET