In c#, the string Contains method is used to check whether the specified substring exists in the given string or not, and it will return a boolean value.
If a substring exists in a string, then the contains method will return true otherwise, it will return false.
Following is the pictorial representation of checking whether the substring exists in a given string or not using the Contains() method in the c# programming language.
If you observe the above diagram, we are checking whether the “Welcome” text exists in the “Welcome to Tutlane” string or not using the Contains method, and it returns a true because the “Welcome” substring exists in the given string.
Following is the syntax of defining a Contains method to check whether the substring exists in a given string or not in the c# programming language.
If you observe syntax, the Contains method will check whether the substring value exists or not, and it will return a boolean value.
Following is the example of using Contains() method to check whether the given value occurs within the string or not in the c# programming language.
If you observe the above example, we used a Contains() method to determine whether the given substring value is found in the string or not and return a bool value.
When you execute the above c# program, you will get the result below.
If you observe the above result, the Contains() method performs a case-sensitive operation that’s the reason for the “Tutlane” substring it returns “true” and for the “tutlane” substring, it returns false.
To perform a case-insensitive string comparison, you need to use the string IndexOf method. Following is the example of performing a case-insensitive search in the c# programming language.
If you observe the above example, we used a string IndexOf method to perform a case insensitive search. We will learn more about the string IndexOf method in the next chapters.
When you execute the above c# program, you will get the result below.
If you observe the above result, for both substrings “Tutlane” and “tutlane” we got a result as true.
This is how we can use Contains() method to check whether the specified substring exists in the given string or not in the c# programming language.