String.Contains Method

Definition

Overloads

Contains(String, StringComparison)

Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules.

Contains(Char, StringComparison)

Returns a value indicating whether a specified character occurs within this string, using the specified comparison rules.

Contains(Char)

Returns a value indicating whether a specified character occurs within this string.

Contains(String)

Returns a value indicating whether a specified substring occurs within this string.

Contains(String, StringComparison)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules.

public bool Contains (string value, StringComparison comparisonType);

Parameters

value
String

The string to seek.

comparisonType
StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

Returns

true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Contains(Char, StringComparison)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

Returns a value indicating whether a specified character occurs within this string, using the specified comparison rules.

public bool Contains (char value, StringComparison comparisonType);

Parameters

value
Char

The character to seek.

comparisonType
StringComparison

One of the enumeration values that specifies the rules to use in the comparison.

Returns

true if the value parameter occurs within this string; otherwise, false.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Contains(Char)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

Returns a value indicating whether a specified character occurs within this string.

public bool Contains (char value);

Parameters

value
Char

The character to seek.

Returns

true if the value parameter occurs within this string; otherwise, false.

Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Contains(String)

Source:
String.Searching.cs
Source:
String.Searching.cs
Source:
String.Searching.cs

Returns a value indicating whether a specified substring occurs within this string.

public bool Contains (string value);

Parameters

value
String

The string to seek.

Returns

true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

Exceptions

value is null.

Examples

The following example determines whether the string "fox" is a substring of a familiar quotation. If "fox" is found in the string, it also displays its starting position.

string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b = s1.Contains(s2);
Console.WriteLine("'{0}' is in the string '{1}': {2}",
                s2, s1, b);
if (b) {
    int index = s1.IndexOf(s2);
    if (index >= 0)
        Console.WriteLine("'{0} begins at character position {1}",
                      s2, index + 1);
}
// This example displays the following output:
//    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
//    'fox begins at character position 17

Remarks

This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.

To perform a culture-sensitive or ordinal case-insensitive comparison:

  • On .NET Core 2.1 and later versions: Call the Contains(String, StringComparison) overload instead.

  • On .NET Framework: Create a custom method. The following example illustrates one such approach. It defines a String extension method that includes a StringComparison parameter and indicates whether a string contains a substring when using the specified form of string comparison.

using System;

public static class StringExtensions
{
   public static bool Contains(this String str, String substring, 
                               StringComparison comp)
   {                            
        if (substring == null)
            throw new ArgumentNullException("substring", 
                                         "substring cannot be null.");
        else if (! Enum.IsDefined(typeof(StringComparison), comp))
            throw new ArgumentException("comp is not a member of StringComparison",
                                     "comp");

        return str.IndexOf(substring, comp) >= 0;                      
   }
}
String s = "This is a string.";
String sub1 = "this";
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1);
StringComparison comp = StringComparison.Ordinal;
Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));

comp = StringComparison.OrdinalIgnoreCase;
Console.WriteLine("   {0:G}: {1}", comp, s.Contains(sub1, comp));

// The example displays the following output:
//       Does 'This is a string.' contain 'this'?
//          Ordinal: False
//          OrdinalIgnoreCase: True

If you are interested in the position of the substring value in the current instance, you can call the IndexOf method to get the starting position of its first occurrence, or you can call the LastIndexOf method to get the starting position of its last occurrence. The example includes a call to the IndexOf(String) method if a substring is found in a string instance.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0