![]() |
VOOZH | about |
In C#, the String.Contains() method is used to check whether a substring exists within a given string. It returns a Boolean value (true or false) indicating whether the substring is found. By default, the method performs a case-sensitive comparison.
Example 1: Here, we are using the String.Contains() method to check if the string contains the specified substring (Case-Sensitive Comparison).
is 'for' is present in the 'GeeksforGeeks': True is 'For' is present in the 'GeeksforGeeks': False
public bool Contains(string str)
Note: This method performs the case-sensitive checking. The search will always begin from the first character position of the string and continue until the last character position.
Example 2: Use the Contains() method to check the starting index of a substring. If the substring is found, you can also determine its starting index using String.IndexOf().
'gfg' is in the string 'Welcome to gfg': True gfg begins at character position 12
Example 3: Check whether the substring is present in a string using ordinal comparison and case-insensitive ordinal comparison.
is 'For' is present in the 'GeeksforGeeks': False is 'For' is present in the 'GeeksforGeeks': True