StringCollection.Contains(String) 메서드

정의

지정한 문자열이 StringCollection에 있는지 여부를 확인합니다.Determines whether the specified string is in the StringCollection.

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

StringCollection에서 찾을 문자열입니다.The string to locate in the StringCollection. 값은 null이 될 수 있습니다.The value can be null.

반환

truevalue에 있으면 StringCollection이고, 그렇지 않으면 false입니다.true if value is found in the StringCollection; otherwise, false.

예제

다음 코드 예제에서는 StringCollection에서 요소를 검색 합니다.The following code example searches the StringCollection for an element.

#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 메서드는 추가 작업을 수행 하기 전에 문자열이 있는지 확인할 수 있습니다.The Contains method can confirm the existence of a string before performing further operations.

이 메서드를 호출 하 여 같은지 확인 Object.Equals합니다.This method determines equality by calling Object.Equals. 문자열 비교 대/소문자를 구분 하지 않습니다.String comparisons are case-sensitive.

이 메서드는 선형 검색을 수행 합니다. 따라서이 메서드는 O (n) 작업으로, n Count됩니다.This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

.NET Framework 2.0부터이 메서드는 item의 컬렉션 개체 EqualsCompareTo 메서드를 사용 하 여 항목이 있는지 여부를 확인 합니다.Starting with the .NET Framework 2.0, this method uses the collection's objects' Equals and CompareTo methods on item to determine whether item exists. 이전 버전의 .NET Framework에서는 컬렉션의 개체에 대 한 item 매개 변수의 EqualsCompareTo 메서드를 사용 하 여 이러한 결정을 수행 했습니다.In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item parameter on the objects in the collection.

적용 대상

추가 정보