String.Contains 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Contains(Rune, StringComparison) | |
| Contains(String, StringComparison) |
回傳一個值,表示指定字串是否出現在該字串中,並使用指定的比較規則。 |
| Contains(Char, StringComparison) |
回傳一個值,表示指定字元是否出現在該字串中,並使用指定的比較規則。 |
| Contains(String) |
回傳一個值,表示指定子字串是否出現在此字串中。 |
| Contains(Char) |
回傳一個值,表示指定字元是否出現在該字串中。 |
| Contains(Rune) |
Contains(Rune, StringComparison)
public:
bool Contains(System::Text::Rune value, StringComparison comparisonType);
public bool Contains(System.Text.Rune value, StringComparison comparisonType);
member this.Contains : System.Text.Rune * StringComparison -> bool
Public Function Contains (value As Rune, comparisonType As StringComparison) As Boolean
參數
- value
- Rune
- comparisonType
- StringComparison
傳回
適用於
Contains(String, StringComparison)
回傳一個值,表示指定字串是否出現在該字串中,並使用指定的比較規則。
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)
回傳一個值,表示指定字元是否出現在該字串中,並使用指定的比較規則。
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
其中一個列舉值,用來指定比較時要使用的規則。
傳回
true若參數value位於此字串內;否則,。 false
適用於
Contains(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
那條線要尋找。
傳回
true若參數value出現在此字串內,或value為空字串(“”);否則,。 false
例外狀況
value 為 null。
範例
以下範例判斷字串「fox」是否為熟悉引號的子字串。 若字串中出現「fox」,則會顯示起始位置。
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) 方法。
另請參閱
適用於
Contains(Char)
回傳一個值,表示指定字元是否出現在該字串中。
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
值得尋找的角色。
傳回
true若參數value位於此字串內;否則,。 false
備註
此方法進行序數比較(大小寫區分且不影響文化)。