C# String.Contains() – Examples
String.Contains() method is used to returns a value indicating whether a specified character occurs within this string.
In this tutorial, we will learn about the syntax of C# String.Contains() method, and learn how to use this method with the help of examples.
Contains(Char)
String.Contains() returns True if specified character occurs within this string, else it returns False.
Syntax
The syntax of Contains() method with Char parameter is
String.Contains(Char value)
where
Parameter | Description |
---|---|
value | The character to check if present in this string. |
Return Value
This method returns boolean value.
Example 1 – Contains(Char)
In this example, we will take a string str
and a character value
. We will use String.Contains(Char) method to check if the character value
is present in string str
.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdefgh";
Char value = 'c';
Boolean result = str.Contains(value);
Console.WriteLine($"Does string contain specified character? {result}");
}
}
Output
Does string contain specified character? True
Example 2 – Contains(Char) – Char not Present in String
In this example, we will take a string str
and a character value
such that this character value
is not present in str
. Now, if we call Contains() method on string str
with value
passed as argument, the method returns False.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdefgh";
Char value = 'm';
Boolean result = str.Contains(value);
Console.WriteLine($"Does string contain specified character? {result}");
}
}
Output
Does string contain specified character? False
Contains(Char, StringComparison)
String.Contains() returns a boolean value indicating whether a specified character is present in this string, using the specified string comparison rules.
Syntax
The syntax of Contains() method with Char and StringComparison parameters is
String.Contains(Char value, StringComparison comp)
where
Parameter | Description |
---|---|
value | The character to check if present in this string. |
comp | The StringComparison object specifying the rules for comparison. |
Return Value
This method returns boolean value.
Example 3 – Contains(value, comp)
In this example, we will check if character value
is present in the string str
with a rule to ignore the case while making the string comparison.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdefgh";
Char value = 'E';
StringComparison comp = StringComparison.OrdinalIgnoreCase;
Boolean result = str.Contains(value, comp);
Console.WriteLine($"Does string contain specified character? {result}");
}
}
Output
Does string contain specified character? True
Contains(String)
String.Contains() returns a boolean value indicating whether a specified substring is present in this string. If the substring is present in the given string, then it returns True, else it returns False.
Syntax
The syntax of Contains() method with substring passed as parameter is
String.Contains(String value)
where
Parameter | Description |
---|---|
value | The substring to check if present in this string. |
Return Value
This method returns boolean value.
Example 4 – Contains(substring)
In this example, we will take a string str
and a substring value
. We will use String.Contains(String) method to check if the substring value
is present in string str
.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdefgh";
String value = "cd";
Boolean result = str.Contains(value);
Console.WriteLine($"Does string contain specified substring? {result}");
}
}
Output
Does string contain specified substring? True
Contains(String, StringComparison)
String.Contains() returns a value indicating whether a specified string occurs within this string, using the specified comparison rules.
Syntax
The syntax of Contains() method with substring and StringComparison objects as parameters is
String.Contains(String value, StringComparison comp)
where
Parameter | Description |
---|---|
value | The substring to check if present in this string. |
comp | The StringComparison object specifying the rules for comparison. |
Return Value
This method returns boolean value.
Example 5 – Contains(substring, comp)
In this example, we will check if substring value
is present in the string str
with a rule to ignore the case while making the string comparison.
C# Program
using System;
class Example {
static void Main(string[] args) {
String str = "abcdefgh";
String value = "CD";
StringComparison comp = StringComparison.OrdinalIgnoreCase;
Boolean result = str.Contains(value, comp);
Console.WriteLine($"Does string contain specified substring? {result}");
}
}
Output
Does string contain specified substring? True
Conclusion
In this C# Tutorial, we have learnt the syntax of C# String.Contains() method, and also learnt how to use this method with the help of C# example programs.