StringCollection.Remove(String) 메서드

정의

StringCollection에서 맨 처음 발견되는 특정 문자열을 제거합니다.

public:
 void Remove(System::String ^ value);
public void Remove (string value);
public void Remove (string? value);
member this.Remove : string -> unit
Public Sub Remove (value As String)

매개 변수

value
String

StringCollection에서 제거할 문자열입니다. 값은 null이 될 수 있습니다.

예제

다음 코드 예제에서는 에서 요소를 제거 합니다 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 );
   
   // Removes one element from the StringCollection.
   myCol->Remove( "yellow" );
   Console::WriteLine( "After removing \"yellow\":" );
   PrintValues( myCol );
   
   // Removes all occurrences of a value from the StringCollection.
   int i = myCol->IndexOf( "RED" );
   while ( i > -1 )
   {
      myCol->RemoveAt( i );
      i = myCol->IndexOf( "RED" );
   }

   Console::WriteLine( "After removing all occurrences of \"RED\":" );
   PrintValues( myCol );
   
   // Clears the entire collection.
   myCol->Clear();
   Console::WriteLine( "After clearing the collection:" );
   PrintValues( myCol );
}

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

After removing "yellow":
   RED
   orange
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After removing all occurrences of "RED":
   orange
   green
   blue
   indigo
   violet

After clearing the collection:

*/
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 );

      // Removes one element from the StringCollection.
      myCol.Remove( "yellow" );

      Console.WriteLine( "After removing \"yellow\":" );
      PrintValues( myCol );

      // Removes all occurrences of a value from the StringCollection.
      int i = myCol.IndexOf( "RED" );
      while ( i > -1 )  {
         myCol.RemoveAt( i );
         i = myCol.IndexOf( "RED" );
      }

      Console.WriteLine( "After removing all occurrences of \"RED\":" );
      PrintValues( myCol );

      // Clears the entire collection.
      myCol.Clear();

      Console.WriteLine( "After clearing the collection:" );
      PrintValues( myCol );
   }

   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

After removing "yellow":
   RED
   orange
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After removing all occurrences of "RED":
   orange
   green
   blue
   indigo
   violet

After clearing the collection:

*/
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)

      ' Removes one element from the StringCollection.
      myCol.Remove("yellow")

      Console.WriteLine("After removing ""yellow"":")
      PrintValues(myCol)

      ' Removes all occurrences of a value from the StringCollection.
      Dim i As Integer = myCol.IndexOf("RED")
      While i > - 1
         myCol.RemoveAt(i)
         i = myCol.IndexOf("RED")
      End While

      Console.WriteLine("After removing all occurrences of ""RED"":")
      PrintValues(myCol)

      ' Clears the entire collection.
      myCol.Clear()

      Console.WriteLine("After clearing the collection:")
      PrintValues(myCol)

   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
'
'After removing "yellow":
'   RED
'   orange
'   RED
'   green
'   blue
'   RED
'   indigo
'   violet
'   RED
'
'After removing all occurrences of "RED":
'   orange
'   green
'   blue
'   indigo
'   violet
'
'After clearing the collection:
'

설명

중복 문자열은 에서 StringCollection허용됩니다. 첫 번째 항목만 제거됩니다. 지정된 문자열의 모든 발생을 제거하려면 를 반복적으로 사용 RemoveAt(IndexOf(value)) 하지만 IndexOf 는 -1을 반환하지 않습니다.

StringCollection 지정된 개체가 포함되어 있지 않으면 는 StringCollection 변경되지 않은 상태로 유지됩니다. 예외는 throw되지 않습니다.

목록처럼 연속된 요소로 구성된 컬렉션에서는 제거된 요소 다음에 있는 요소를 위로 이동하여 빈 자리를 채웁니다. 컬렉션에 인덱스가 지정된 경우 이동되는 요소의 인덱스도 업데이트됩니다. 요소가 해시 테이블 같은 버킷으로 개념적으로 그룹화되어 있는 컬렉션에는 이러한 동작이 적용되지 않습니다.

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

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

적용 대상

추가 정보