C# String Contains() Method
Last Updated : 06 Mar, 2025
In C#, the String.Contains() method is used to check whether a substring exists within a given string. It returns a Boolean value (true or false) indicating whether the substring is found. By default, the method performs a case-sensitive comparison.
Example 1: Here, we are using the String.Contains() method to check if the string contains the specified substring (Case-Sensitive Comparison).
C# // C# program to demonstrate the // String.Contains() Method using System; class Geeks { public static void Main() { // declaring the string String str = "GeeksforGeeks"; String s1 = "for"; String s2 = "For"; bool ans; // using String.Contains() Method // Here case-sensitive comparison ans = str.Contains(s1); Console.WriteLine($"is '{s1}' is present in the '{str}': {ans}"); // Here case-insensitive comparison // return false because 'For' is not present in the 'GeeksforGeeks' ans = str.Contains(s2); Console.WriteLine($"is '{s2}' is present in the '{str}': {ans}"); } }
Outputis 'for' is present in the 'GeeksforGeeks': True is 'For' is present in the 'GeeksforGeeks': False
Syntax of String.Contains() Method
public bool Contains(string str)
- Parameter: It takes a single parameter str (string) which is to be checked. Type of this parameter is System.String.
- Return Type: Returns a boolean value. If a substring exists in a string or the value is the empty string (“”), then it returns True, otherwise returns False.
- Exception: This method can give ArgumentNullException if str is null.
Note: This method performs the case-sensitive checking. The search will always begin from the first character position of the string and continue until the last character position.
Example 2: Use the Contains() method to check the starting index of a substring. If the substring is found, you can also determine its starting index using String.IndexOf().
C# // C# program to demonstrate the // String.Contains() Method // along with the starting position using System; class Geeks { public static void Main() { string str = "Welcome to gfg"; string sub = "gfg"; // Check if the substring is // present in the main string bool b = str.Contains(sub); Console.WriteLine("'{0}' is in the string '{1}': {2}", sub, str, b); if (b) { int index = str.IndexOf(sub); if (index >= 0) Console.WriteLine("{0} begins at character position {1}", sub, index + 1); } } }
Output'gfg' is in the string 'Welcome to gfg': True gfg begins at character position 12
Example 3: Check whether the substring is present in a string using ordinal comparison and case-insensitive ordinal comparison.
C# // C# program to demonstrate the // String.Contains() Method using System; class Geeks { public static void Main() { // declaring the string String str = "GeeksforGeeks"; String sub = "For"; bool ans; // Here case-insensitive comparison // return false because 'For' is not present in the 'GeeksforGeeks' ans = str.Contains(sub); Console.WriteLine($"is '{sub}' is present in the '{str}': {ans}"); // Ordinal Ignore Case comparison ans = str.Contains(sub, StringComparison.OrdinalIgnoreCase); Console.WriteLine($"is '{sub}' is present in the '{str}': {ans}"); } }
Outputis 'For' is present in the 'GeeksforGeeks': False is 'For' is present in the 'GeeksforGeeks': True
Similar Reads
C# | String.Contains() Method In C#, the String.Contains() method is used to check whether a substring exists within a given string. It returns a Boolean value (true or false) indicating whether the substring is found. By default, the method performs a case-sensitive comparison.Example 1: Here, we are using the String.Contains()
3 min read
std::string::compare() in C++ The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to
4 min read
Java Vector contains() Method The contains() method in Java is used to check whether a specific element is present in the Vector or not.Example 1: In this example, we will check whether a particular string is present in the vector or not.Java// Java program to demonstrate the use // of the contains() method with Strings import j
3 min read
C# | Char.IsSurrogate(String, Int32) Method This method is used to indicates whether the character at the specified position in a specified string has a surrogate code unit or not. Syntax: public static bool IsSurrogate (string s, int index); Parameters: s: It is a String. index: It is the character position to evaluate in s. Return Value: Th
4 min read
Java Vector containsAll() Method In Java, the containsAll() method is used to check if the Vector contains all the elements of a specified Collection or not.Example 1: Below program demonstrating the use of containsAll() method with Strings.Java// Java Program to demonstrate the use of // containsAll() with Strings import java.util
2 min read
C# | Char.IsSurrogatePair(String, Int32) Method This method is used to indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair or not. Syntax: public static bool IsSurrogatePair (string s, int index); Parameters: s: It is a String. index: It is the starting position of the pair of characters to evalua
4 min read
C# String Contains() Method min read