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() method to check if the string contains the specified substring (Case-Sensitive Comparison).
// 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}");
}
}
Output
is '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# 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# 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}");
}
}
Output
is 'For' is present in the 'GeeksforGeeks': False is 'For' is present in the 'GeeksforGeeks': True