String.Contains 方法

定义

重载

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)

使用指定的比较规则返回一个值,该值指示指定的字符串是否出现在此字符串中。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.

public:
 bool Contains(char value, StringComparison comparisonType);
public bool Contains (char value, StringComparison comparisonType);
member this.Contains : char * StringComparison -> bool
Public Function Contains (value As Char, comparisonType As StringComparison) As Boolean

参数

value
Char

要查找的字符。The character to seek.

comparisonType
StringComparison

一个枚举值,用于指定比较中要使用的规则。One of the enumeration values that specifies the rules to use in the comparison.

返回

如果 value 参数在此字符串中出现,则为 true;否则为 falsetrue if the value parameter occurs within this string; otherwise, false.

Contains(Char)

返回一个值,该值指示指定的字符是否出现在此字符串中。Returns a value indicating whether a specified character occurs within this string.

public:
 bool Contains(char value);
public bool Contains (char value);
member this.Contains : char -> bool
Public Function Contains (value As Char) As Boolean

参数

value
Char

要查找的字符。The character to seek.

返回

如果 value 参数在此字符串中出现,则为 true;否则为 falsetrue if the value parameter occurs within this string; otherwise, false.

Contains(String)

返回一个值,该值指示指定的子串是否出现在此字符串中。Returns a value indicating whether a specified substring occurs within this string.

public:
 bool Contains(System::String ^ value);
public bool Contains (string value);
member this.Contains : string -> bool
Public Function Contains (value As String) As Boolean

参数

value
String

要搜寻的字符串。The string to seek.

返回

如果 true 参数出现在此字符串中,或者 value 为空字符串 (""),则为 value;否则为 falsetrue if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

异常

valuenullvalue is null.

示例

下面的示例确定字符串 "fox" 是否为熟悉的引号的子字符串。The following example determines whether the string "fox" is a substring of a familiar quotation. 如果在字符串中找到 "fox",则它还会显示其起始位置。If "fox" is found in the string, it also displays its starting position.

using namespace System;

int main()
{
   String^ s1 = "The quick brown fox jumps over the lazy dog";
   String^ s2 = "fox";
   bool b = s1->Contains( s2 );
   Console::WriteLine( "Is the string, s2, in the string, s1?: {0}", 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
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 display the following output:
//    'fox' is in the string 'The quick brown fox jumps over the lazy dog': True
//    'fox begins at character position 17
Class Example
   Public Shared Sub Main()
      Dim s1 As String = "The quick brown fox jumps over the lazy dog"
      Dim s2 As String = "fox"
      Dim b As Boolean = s1.Contains(s2)
      Console.WriteLine("'{0}' is in the string '{1}': {2}",
                        s2, s1, b)
      If b Then
          Dim index As Integer = s1.IndexOf(s2)
          If index >= 0 Then
             Console.WriteLine("'{0} begins at character position {1}",
                               s2, index + 1)
          End If
       End If
   End Sub
End Class
'
' 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

注解

此方法执行序号(区分大小写和不区分区域性)比较。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 determine whether a string contains a specified substring by using something other than ordinal comparison (such as culture-sensitive comparison, or ordinal case-insensitive comparison), you can create a custom method. 下面的示例阐释了这种方法。The following example illustrates one such approach. 它定义了一个 String 扩展方法,该方法包含一个 StringComparison 参数,并指示当使用指定的字符串比较形式时,字符串是否包含子字符串。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;                      
   }
}
Imports System.Runtime.CompilerServices

Module StringExtensions
   <Extension()>
   Public Function Contains(str As String, substring As String, 
                            comp As StringComparison) As Boolean
      If substring Is Nothing Then
         Throw New ArgumentNullException("substring", 
                                         "substring cannot be null.")
      Else If Not [Enum].IsDefined(GetType(StringComparison), comp)
         Throw New ArgumentException("comp is not a member of StringComparison",
                                     "comp")
      End If                               
      Return str.IndexOf(substring, comp) >= 0                      
   End Function
End Module

然后,在使用序号比较和不区分大小写的序号比较时,下面的示例调用 Contains 扩展方法来确定字符串中是否存在子字符串。The following example then calls the Contains extension method to determine whether a substring is found in a string when using ordinal comparison and case-insensitive ordinal comparison.

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
Public Module Example
   Public Sub Main
      Dim s As String = "This is a string."
      Dim sub1 As String = "this"
      Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1)
      Dim comp As StringComparison = 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))
   End Sub
End Module
' The example displays the following output:
'       Does 'This is a string.' contain 'this'?
'          Ordinal: False
'          OrdinalIgnoreCase: True

如果您对当前实例中的子字符串 value 的位置感兴趣,则可以调用 IndexOf 方法获取其第一次出现的起始位置,也可以调用 LastIndexOf 方法获取其最后一个匹配项的起始位置。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. 如果在字符串实例中找到子字符串,则该示例包括对 IndexOf(String) 方法的调用。The example includes a call to the IndexOf(String) method if a substring is found in a string instance.

另请参阅

Contains(String, StringComparison)

使用指定的比较规则返回一个值,该值指示指定的字符串是否出现在此字符串中。Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules.

public:
 bool Contains(System::String ^ value, StringComparison comparisonType);
public bool Contains (string value, StringComparison comparisonType);
member this.Contains : string * StringComparison -> bool
Public Function Contains (value As String, comparisonType As StringComparison) As Boolean

参数

value
String

要搜寻的字符串。The string to seek.

comparisonType
StringComparison

一个枚举值,用于指定比较中要使用的规则。One of the enumeration values that specifies the rules to use in the comparison.

返回

如果 true 参数出现在此字符串中,或者 value 为空字符串 (""),则为 value;否则为 falsetrue if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

适用于