String.Contains 方法

定义

重载

Contains(String, StringComparison)

使用指定的比较规则返回一个值,该值指示指定的字符串是否出现在此字符串中。

Contains(Char, StringComparison)

使用指定的比较规则返回一个值,该值指示指定的字符是否出现在此字符串中。

Contains(String)

返回一个值,该值指示指定的子串是否出现在此字符串中。

Contains(Char)

返回一个值,该值指示指定的字符是否出现在此字符串中。

Contains(String, StringComparison)

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

使用指定的比较规则返回一个值,该值指示指定的字符串是否出现在此字符串中。

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

要搜寻的字符串。

comparisonType
StringComparison

一个枚举值,用于指定比较中要使用的规则。

返回

如果 true 参数出现在此字符串中,或者 value 为空字符串 (""),则为 value;否则为 false

适用于

Contains(Char, StringComparison)

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

使用指定的比较规则返回一个值,该值指示指定的字符是否出现在此字符串中。

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

要查找的字符。

comparisonType
StringComparison

一个枚举值,用于指定比较中要使用的规则。

返回

如果 value 参数在此字符串中出现,则为 true;否则为 false

适用于

Contains(String)

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

返回一个值,该值指示指定的子串是否出现在此字符串中。

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

要搜寻的字符串。

返回

如果 true 参数出现在此字符串中,或者 value 为空字符串 (""),则为 value;否则为 false

例外

valuenull

示例

以下示例确定字符串“fox”是否是熟悉的引号的子字符串。 如果在字符串中找到“fox”,则它还会显示其起始位置。

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 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
let s1 = "The quick brown fox jumps over the lazy dog"
let s2 = "fox"
let b = s1.Contains s2
printfn $"'{s2}' is in the string '{s1}': {b}"
if b then
    let index = s1.IndexOf s2
    if index >= 0 then
        printfn $"'{s2} begins at character position {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
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

注解

此方法执行序号 (区分大小写和不区分区域性的) 比较。 搜索从此字符串的第一个字符位置开始,并一直持续到最后一个字符位置。

执行区分区域性或不区分大小写的序号比较:

  • 在 .NET Core 2.1 及更高版本上:请改为调用 Contains(String, StringComparison) 重载。

  • 在.NET Framework:Create自定义方法。 以下示例演示了一种此类方法。 它定义一个 String 包含 StringComparison 参数的扩展方法,并指示在使用指定形式的字符串比较时,字符串是否包含子字符串。

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;                      
   }
}
open System
open System.Runtime.CompilerServices

[<Extension>]
type StringExtensions =
    [<Extension>]
    static member Contains(str: string, substring, comp: StringComparison) =
        if substring = null then
            invalidArg "substring" "substring cannot be null"
        if Enum.IsDefined(typeof<StringComparison>, comp) |> not then
            invalidArg "comp" "comp is not a member of StringComparison"
        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
let s = "This is a string."
let sub1 = "this"
printfn $"Does '{s}' contain '{sub1}'?"
let comp = StringComparison.Ordinal
printfn $"   {comp:G}: {s.Contains(sub1, comp)}"

let comp2 = StringComparison.OrdinalIgnoreCase
printfn $"   {comp2:G}: {s.Contains(sub1, comp2)}"

// The example displays the following output:
//       Does 'This is a string.' contain 'this'?
//          Ordinal: False
//          OrdinalIgnoreCase: True
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
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 方法以获取其最后一个匹配项的起始位置。 该示例包括对 方法的调用( IndexOf(String) 如果在字符串实例中找到子字符串)。

另请参阅

适用于

Contains(Char)

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

返回一个值,该值指示指定的字符是否出现在此字符串中。

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

要查找的字符。

返回

如果 value 参数在此字符串中出现,则为 true;否则为 false

注解

此方法执行序号 (区分大小写和不区分区域性的) 比较。

适用于