Trending October 2023 # Top 2 Examples Of C# Contains() Method # Suggested November 2023 # Top 12 Popular | Restaurant12h.com

Trending October 2023 # Top 2 Examples Of C# Contains() Method # Suggested November 2023 # Top 12 Popular

You are reading the article Top 2 Examples Of C# Contains() Method updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Top 2 Examples Of C# Contains() Method

Introduction to C# Contains() Method

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.

Web development, programming languages, Software testing & others

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; public class program { public static void Main(string[] args) { string st1 = "C Sharp"; string st2 = " "; string st3 = "Welcome"; 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)); } }

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.

using System; public class program { public static void Main(string[] args) { string st1 = "Welcome to C Sharp"; string st2 = "#"; string st3 = "Welcome"; 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:

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 –

You're reading Top 2 Examples Of C# Contains() Method

Update the detailed information about Top 2 Examples Of C# Contains() Method on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!