StringCollection.Contains(String) 메서드

정의

지정한 문자열이 StringCollection에 있는지 여부를 확인합니다.

public:
 bool Contains(System::String ^ value);
public bool Contains (string value);
public bool Contains (string? value);
member this.Contains : string -> bool
Public Function Contains (value As String) As Boolean

매개 변수

value
String

StringCollection에서 찾을 문자열입니다. 값은 null이 될 수 있습니다.

반환

truevalue에 있으면 StringCollection이고, 그렇지 않으면 false입니다.

예제

다음 코드 예제에서는 요소를 검색 StringCollection 합니다.

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintValues( IEnumerable^ myCol );
int main()
{
   
   // Creates and initializes a new StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
   myCol->AddRange( myArr );
   Console::WriteLine( "Initial contents of the StringCollection:" );
   PrintValues( myCol );
   
   // Checks whether the collection contains "orange" and, if so, displays its index.
   if ( myCol->Contains( "orange" ) )
      Console::WriteLine( "The collection contains \"orange\" at index {0}.", myCol->IndexOf( "orange" ) );
   else
      Console::WriteLine( "The collection does not contain \"orange\"." );
}

void PrintValues( IEnumerable^ myCol )
{
   IEnumerator^ myEnum = myCol->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   {0}", obj );
   }

   Console::WriteLine();
}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The collection contains "orange" at index 1.

*/
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Checks whether the collection contains "orange" and, if so, displays its index.
      if ( myCol.Contains( "orange" ) )
         Console.WriteLine( "The collection contains \"orange\" at index {0}.", myCol.IndexOf( "orange" ) );
      else
         Console.WriteLine( "The collection does not contain \"orange\"." );
   }

   public static void PrintValues( IEnumerable myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }
}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The collection contains "orange" at index 1.

*/
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringCollection   

   Public Shared Sub Main()

      ' Creates and initializes a new StringCollection.
      Dim myCol As New StringCollection()
      Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
      myCol.AddRange(myArr)

      Console.WriteLine("Initial contents of the StringCollection:")
      PrintValues(myCol)

      ' Checks whether the collection contains "orange" and, if so, displays its index.
      If myCol.Contains("orange") Then
         Console.WriteLine("The collection contains ""orange"" at index {0}.", myCol.IndexOf("orange"))
      Else
         Console.WriteLine("The collection does not contain ""orange"".")
      End If 

   End Sub

   Public Shared Sub PrintValues(myCol As IEnumerable)
      Dim obj As [Object]
      For Each obj In  myCol
         Console.WriteLine("   {0}", obj)
      Next obj
      Console.WriteLine()
   End Sub

End Class


'This code produces the following output.
'
'Initial contents of the StringCollection:
'   RED
'   orange
'   yellow
'   RED
'   green
'   blue
'   RED
'   indigo
'   violet
'   RED
'
'The collection contains "orange" at index 1.
'

설명

메서드는 Contains 추가 작업을 수행하기 전에 문자열의 존재를 확인할 수 있습니다.

이 메서드를 호출 하 여 같은지 확인 Object.Equals합니다. 문자열 비교는 대/소문자를 구분합니다.

이 메서드는 선형 검색을 수행합니다. 따라서 이 메서드는 O(n) 작업이며 여기서 n 는 입니다 Count.

.NET Framework 2.0부터 이 메서드는 컬렉션의 개체 EqualsCompareTo 메서드를 item 사용하여 항목이 있는지 여부를 확인합니다. 이전 버전의 .NET Framework 컬렉션의 개체에서 매개 변수의 itemCompareTo 메서드를 사용하여 Equals 이 결정을 내렸습니다.

적용 대상

추가 정보