VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-string-contains-method/

⇱ C# String Contains() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# String Contains() Method

Last Updated : 11 Jul, 2025

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).


Output
is 'for' is present in the 'GeeksforGeeks': True
is 'For' is present in the 'GeeksforGeeks': False

Syntax of String.Contains() Method

public bool Contains(string str)

  • Parameter: It takes a single parameter str (string) which is to be checked. Type of this parameter is System.String.
  • Return Type: Returns a boolean value. If a substring exists in a string or the 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 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().


Output
'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.


Output
is 'For' is present in the 'GeeksforGeeks': False
is 'For' is present in the 'GeeksforGeeks': True
Comment
Article Tags:

Explore