StringCollection.Contains(String) 메서드

정의

지정된 문자열이 .에 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. 값은 .일 null수 있습니다.

반품

있으면 , 그렇지 않으면 .입니다.

예제

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

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.

이 메서드는 컬렉션의 개체 Equals 와 메서드를 item 사용하여 존재하는지 여부를 item 확인 CompareTo 합니다.

적용 대상

추가 정보