Contains ExamplesΒΆ

The general syntax for this method is:

stringName.Contians(substr);

Given a candidate substring, this method returns the boolean value if the substring is contained within the string. Can also be used to locate characters within a string.

Example

1
2
3
Console.WriteLine("LaunchCode".Contains("C"));

Console.WriteLine("LaunchCode".Contains("A"));

Output

True
False

Example

An email address must contain an @ symbol. Checking for the presence of this symbol is a part of email address verification in most programs.

1
2
3
4
5
6
7
8
string input = "[email protected]";
bool validEmail = input.Contains('@');

if (validEmail) {
   Console.WriteLine("Email contains @");
} else {
   Console.WriteLine("Invalid email");
}

Output

Email contains @