String.Contains String.Contains String.Contains String.Contains Method

定義

オーバーロード

Contains(Char) Contains(Char) Contains(Char) Contains(Char)
Contains(String) Contains(String) Contains(String) Contains(String)

指定した部分文字列がこの文字列内に存在するかどうかを示す値を返します。Returns a value indicating whether a specified substring occurs within this string.

Contains(Char, StringComparison) Contains(Char, StringComparison) Contains(Char, StringComparison) Contains(Char, StringComparison)
Contains(String, StringComparison) Contains(String, StringComparison) Contains(String, StringComparison) Contains(String, StringComparison)

Contains(Char) Contains(Char) Contains(Char) 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 Char Char Char

戻り値

Contains(String) Contains(String) Contains(String) 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 String String String

シークする文字列。The string to seek.

戻り値

value パラメーターがこの文字列内に存在するか、value が空の文字列 ("") の場合は true。それ以外の場合は falsetrue if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

例外

次の例"について 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
using System;

class Example
{
    public static void Main() 
    {
       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. 次の例は、1 つの方法を示しています。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.

using System;

public class Example
{
   public static void Main()
   {
      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(Char, StringComparison) Contains(Char, StringComparison) Contains(Char, StringComparison) 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 Char Char Char

戻り値

Contains(String, StringComparison) Contains(String, StringComparison) Contains(String, 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

パラメーター

戻り値

適用対象