C# | String.Contains() Method

In C#, String.Contains() is a string method. This method is used to check whether the substring occurs within a given string or not.

Syntax:

public bool Contains(string str)

Parameter:

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 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// String.Contains() Method
using System;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // string type
        String str = "GeeksforGeeks";
        String substr1 = "for";
        String substr2 = "For";
  
        // using String.Contains() Method
        Console.WriteLine(str.Contains(substr1));
  
        // Here case-sensitive comparision
        // And substr2 value is 'For'
        // So its return false
        Console.WriteLine(str.Contains(substr2));
    }
}

chevron_right


Output:

True
False

Program 2: To determine whether a substring is present in a string using ordinal comparison and case-insensitive ordinal comparison.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// String.Contains() Method using
// ordinal comparison and case-
// insensitive ordinal comparison
using System;
  
public static class StringExtensions {
  
    // defines a String extension method
    // which includes a StringComparison parameter
    public static bool Contains(this String str,
                                String substr,
                                StringComparison cmp)
    {
        if (substr == null)
            throw new ArgumentNullException("substring substring",
                                            " cannot be null.");
  
        else if (!Enum.IsDefined(typeof(StringComparison), cmp))
            throw new ArgumentException("comp is not a member of",
                                        "StringComparison, comp");
  
        return str.IndexOf(substr, cmp) >= 0;
    }
}
  
// Driver Class
class Geeks {
  
    // Main Method
    public static void Main()
    {
        String str = "GeeksforGeeks";
        String substr = "FOR";
  
        // For Ordinal
        StringComparison comp = StringComparison.Ordinal;
        Console.WriteLine("For {0:G}: {1}", comp,
                          str.Contains(substr, comp));
  
        // for OrdinalIgnoreCase
        comp = StringComparison.OrdinalIgnoreCase;
        Console.WriteLine("For {0:G}: {1}", comp,
                          str.Contains(substr, comp));
    }
}

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// String.Contains() Method
// along with the starting position
using System;
  
class Example {
    public static void Main()
    {
        string sub1 = "GeeksforGeeks is a Computer Science Portal";
        string sub2 = "Computer";
  
        // Check if the substring is
        // present in the main string
        bool b = sub1.Contains(sub2);
  
        Console.WriteLine("'{0}' is in the string '{1}': {2}",
                          sub2, sub1, b);
        if (b) {
            int index = sub1.IndexOf(sub2);
            if (index >= 0)
                Console.WriteLine("{0} begins at character position {1}",
                                  sub2, index + 1);
        }
    }
}

chevron_right


Output:

'Computer' is in the string 'GeeksforGeeks is a Computer Science Portal': True
Computer begins at character position 20

Reference: https://msdn.microsoft.com/en-us/library/system.string.contains



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.