TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Contains String Method

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

Contains. Like IndexOf, Contains searches strings.

It checks if one substring is contained in another. It also provides a case-sensitive ordinal method for checking string contents. Contains returns true or false, not an index.

Example. 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.

Bool

Next: The example program shows that Contains is case-sensitive. It shows how to test the result of Contains.

C# program that uses Contains

using System;

class Program
{
    static void Main()
    {
	Test("Dot Net Perls");
	Test("The Developer Blog");
    }

    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 'deves' lowercase
	//
	if (input.Contains("deves"))
	{
	    Console.WriteLine("Contains 'deves'");
	}
	//
	// See if the string contains 'Dot'
	//
	if (!input.Contains("Dot"))
	{
	    Console.WriteLine("Doesn't Contain 'Dot'");
	}
    }
}

Output

--- Dot Net Perls ---
Contains 'Net': True
--- The Developer Blog ---
Contains 'Net': False
Contains 'deves'
Doesn't Contain 'Dot'

In this example, we call twice the Test method. In that method, the output is printed the console. The string "Net" is first tested with Contains. We show that Contains is case-sensitive.

Testing the result of Contains. The Test method checks Contains in if-conditionals. It shows how to test it for true and false. You also can store the result of Contains in a bool variable.

True, FalseIf

Internals. Contains is actually just a wrapper for IndexOf. When IndexOf returns -1, the string was not found. When Contains cannot find the string, it returns false instead. Contains has no performance advantage over calling IndexOf.

Tip: In IL Disassembler, we see how Contains is implemented. Please check the detailed article.

IL Disassembler Tutorial

Ordinal refers to a position number. 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.

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. This is useful.

Also: Contains is not affected by the system locale, so it is reliable on all systems.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf