String.Contains メソッド

定義

オーバーロード

Contains(Char)

指定した文字がこの文字列内に存在するかどうかを示す値を返します。

Contains(String)

指定した部分文字列がこの文字列内に存在するかどうかを示す値を返します。

Contains(Char, StringComparison)

指定された比較規則を使用して、指定された文字がこの文字列内に含まれるかどうかを示す値を返します。

Contains(String, StringComparison)

指定された比較規則を使用して、指定された文字列がこの文字列内に含まれるかどうかを示す値を返します。

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

検索対象の文字。

戻り値

value パラメーターがこの文字列内で発生する場合は true。それ以外の場合は 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

シークする文字列。

戻り値

value パラメーターがこの文字列内に存在するか、value が空の文字列 ("") の場合は true。それ以外の場合は 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時]: カスタム メソッドを作成します。 次の例は、そのようなアプローチの 1 つを示しています。 指定した形式の 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, 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

比較で使用する規則を指定する列挙値の 1 つ。

戻り値

value パラメーターがこの文字列内で発生する場合は true。それ以外の場合は false

適用対象

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

比較で使用する規則を指定する列挙値の 1 つ。

戻り値

value パラメーターがこの文字列内に存在するか、value が空の文字列 ("") の場合は true。それ以外の場合は false

適用対象