Home  >  Article  >  Backend Development  >  C# Contains()

C# Contains()

WBOY
WBOYOriginal
2024-09-03 15:17:50487browse

The method used to check if a string is present within a given string or not is called Contains() method in C#.If the string is present within the given string, true is returned and if the string is not present within the given string, false is returned and hence the return type is System. Boolean and ArgumentNullException is raised. If the input string which must be checked for the occurrence of the string within it is null and the checking of the string for the occurrence of the given string is based on case sensitivity.

Syntax:

public bool Contains(String string)

Where string is the given string that must be checked for the occurrence of the string within it.

Bool is the Boolean value returned by the Contains method and is set to true if the string is present within the given string and is set to false if the string is not present within the given string.

Working of C# Contains() Method

  • Whenever there is a need to find the occurrence of a string within another string, we make use of the Contains method to check the given string to find the occurrence of another string within it.
  • True is the value returned by the contains() method if the string is present within the given string.
  • False is the value returned by contains() method if the string is not present within the given string.
  • ArgumentNullException is raised if the input string which must be checked for the occurrence of the string within it is null and the checking of the string for the occurrence of the given string is based on case sensitivity.

Examples to Implement C# Contains()

Below are the examples of C# Contains():

Example #1

C# program to demonstrate the Contains() method to check the given string to find the occurrence of the string within a given string.

Code:

using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string which will be checked to find the occurrence of another string within it
string st1 = "C Sharp";
//another string variable is used to store the string which will be checked for in the given string
string st2 = " ";
//another string variable is used to store the string which will be checked for in the given string
string st3 = "Welcome";
//Contains() method is used to check the string for the occurrence of another string within it
Console.WriteLine("If there is occurrence of the string in the given string:{0} ", st1.Contains(st2));
Console.WriteLine("If there is occurrence of the string in the given string:{0} ",st1.Contains(st3));
}
}

Output:

C# Contains()

Explanation: In the above program, a class called program is defined. Then the main method is called within which a string variable is used to store the string which is checked to find the occurrence of another string within it. Then another string variable is used to store the string whose occurrence is searched for in the first string. Then again another string variable is used to store the string whose occurrence is searched for in the first string. Then Contains() method is used to check the string for the occurrence of another string within it. True is returned if there is the occurrence of another string within the first string and false is returned if there is no occurrence of another string within it.

Example #2

C# program to demonstrate the Contains() method to check the given string to find the occurrence of the string within a given string.

Code:

using System;
//a class called program is defined
public class program
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string which will be checked to find the occurrence of another string within it
string st1 = "Welcome to C Sharp";
//another string variable is used to store the string which will be checked for in the given string
string st2 = "#";
//another string variable is used to store the string which will be checked for in the given string
string st3 = "Welcome";
//Contains() method is used to check the string for the occurrence of another string within it
Console.WriteLine("If there is occurrence of the string in the given string:{0} ", st1.Contains(st2));
Console.WriteLine("If there is occurrence of the string in the given string:{0} ",st1.Contains(st3));
}
}

Output:

C# Contains()

Explanation: In the above program, a class called program is defined. Then the main method is called within which a string variable is used to store the string which is checked to find the occurrence of another string within it. Then another string variable is used to store the string whose occurrence is searched for in the first string. Then again another string variable is used to store the string whose occurrence is searched for in the first string. Then Contains() method is used to check the string for the occurrence of another string within it. True is returned if there is an occurrence of another string within the first string and false is returned if there is no occurrence of another string within it.

Conclusion

In this tutorial, we understand the concept of Contains() method in C# through the definition of Contains() method, the syntax of Contains() method and the working of Contains() method through programming examples and their outputs.

Recommended Article

This is a guide to C# Contains(). Here we discuss the Introduction of C# Contains() Method and its working along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –

  1. What is Random Number Generator in C#?
  2. Static Constructor in Java | Working | Applications
  3. TextWriter in C# | Examples
  4. How to Work Static Constructor in C#?

The above is the detailed content of C# Contains(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# String Split()Next article:C# String Split()