C# .Contains()
Published Apr 26, 2023Updated Oct 8, 2024
Contribute to Docs
The .Contains() method determines whether a string includes a particular character or substring. It returns true if the character is included, otherwise the method returns false. There are additional parameters that can modify the comparison rules.
Syntax
// Determines whether the String includes a given character
String.Contains(char);
// Determines whether the String includes a given string
String.Contains(string);
// Determines whether the String includes a given character considering the type of comparison
String.Contains(char, comparisonType)
// Determines whether the String includes a given string considering the type of comparison
String.Contains(string, comparisonType)
.Contains() takes the following arguments:
charis a single character.stringis a sequence of characters.comparisonTypeis an enumeration value that allows to add specific rules to compare strings such as culture, case, and sort. Passing as an additional argument:CurrentCulturedetermines whether strings match culture-sensitive criteria using the current system culture for comparison.CurrentCultureIgnoreCasedoes the same as above and ignores the case.InvariantCulturedetermines whether strings match using a fixed, culture-independent set of rules that remain consistent across all systems and regions.InvariantCultureIgnoreCasedoes the same as above and ignores the case.Ordinaldetermines whether strings match using binary sort rules. This is the fastest comparison method, performing a simple byte-by-byte comparison of Unicode values.OrdinalIgnoreCasedoes the same as above and ignores the case.
Example
The following example shows how we can use .Contains() method
using System;public class Example{public static void Main(){string stringToSeek = "The distance is nothing when one has a motive.";string substring = "motive";char character = 'l';bool result;// String.Contains(string)result = stringToSeek.Contains(substring);Console.WriteLine(result);// String.Contains(char)result = stringToSeek.Contains(character);Console.WriteLine(result);}}
Here is the following output:
TrueFalse
Now, let’s see how we can use the comparisonType parameter to modify the comparison rules:
using System;using System.Globalization;public class Example{public static void Main(){// Turkish culture handles 'i' and 'İ' differentlyCultureInfo turkishCulture = new CultureInfo("tr-TR");string turkishString = "İstanbul";string searchText = "istanbul";// Default culture comparison (typically en-US)bool defaultResult = turkishString.Contains(searchText, StringComparison.CurrentCultureIgnoreCase);// Turkish culture comparisonCultureInfo.CurrentCulture = turkishCulture;bool turkishResult = turkishString.Contains(searchText,StringComparison.CurrentCultureIgnoreCase);Console.WriteLine($"Default culture result: {defaultResult}");Console.WriteLine($"Turkish culture result: {turkishResult}");}}
Here is the output:
Default culture result: TrueTurkish culture result: False
Codebyte Example
The example below determines whether the word helpful is included in the particular string.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours