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. Here we look at how you can use the Contains method on string, using it on dynamic input and then look inside the method and see how it works.
First, 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 of the example program ===
--- Dot Net Perls ---
Contains 'Net': True
--- dot net perls ---
Contains 'Net': False
Contains 'perls'
Doesn't Contain 'Dot'Description of the example. 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.
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 Reflector, we can see how Contains is implemented. This article is based on .NET 3.5 SP1.
public bool Contains(string value)
{
return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}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.
The author has 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.
(See IndexOf String Examples.)
Here we saw how to use the Contains method on the string type in C#. 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.