In C#, String.Contains() is a string method. This method is used to check whether the substring occurs within a given string or not.
public bool Contains(string str)
str: It is the string which is to be checked. Type of this parameter is System.String.
Return Value: It returns the boolean value. If substring exists in string or value is the empty string (“”), then it returns True, otherwise returns False.
Exception: This method can give ArgumentNullException if str is null.
Note: This method performs the case-sensitive checking. The search will always begin from the first character position of the string and continues until the last character position.
Below are the programs illustrate the Contains() Method.
Program 2: To determine whether a substring is present in a string using ordinal comparison and case-insensitive ordinal comparison.
For Ordinal: False For OrdinalIgnoreCase: True
Program 3: The following example determines whether the string “Computer” is a substring of given string. If it is found in the string, it also displays its starting position.
'Computer' is in the string 'GeeksforGeeks is a Computer Science Portal': True Computer begins at character position 20
- Difference between Method Overriding and Method Hiding in C#
- DateTime.Add() Method in C#
- C# | Uri.IsHexEncoding() Method
- Anonymous Method in C#
- C# | Method Overriding
- C# | SortedDictionary.Add() Method
- C# | Method Overloading
- C# | Math.Log() Method
- C# | CompareOrdinal() Method
- C# | StartsWith() Method
- Stack.Pop() Method in C#
- Stack.Contains() Method in C#
- C# | IsNullOrWhiteSpace() Method
- TimeSpan.Add() Method in C#
- Queue.Contains() Method in C#
- C# | ToCharArray() Method
- C# | EndsWith() Method
- C# | CopyTo() Method
- C# | PadRight() Method
- Decimal.Add() Method in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai