相关文章推荐
拉风的眼镜  ·  STRING_AGG ...·  昨天    · 
风流倜傥的麦片  ·  boost::property_tree读取 ...·  14 小时前    · 
豪气的苹果  ·  Android social ...·  11 小时前    · 
寂寞的杯子  ·  opengl ...·  5 月前    · 
干练的香槟  ·  为什么大多数的 C++ ...·  8 月前    · 
bool Contains(char value);
public bool Contains (char value);
member this.Contains : char -> bool
Public Function Contains (value As Char) As Boolean
bool Contains(System::String ^ value);
public bool Contains (string value);
member this.Contains : string -> bool
Public Function Contains (value As String) As Boolean
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:建立自訂方法。 下列範例說明其中一種方法。 它會定義 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)

    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
    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
    即將推出:在 2024 年,我們將隨著內容的意見反應機制逐步淘汰 GitHub 問題,並以新的意見反應系統來取代。 如需詳細資訊,請參閱 https://aka.ms/ContentUserFeedback

    提交並檢視相關的意見反應

  •