StringCollection.Remove(String) Metoda

Definice

Odebere první výskyt konkrétního řetězce z objektu 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)

Parametry

value
String

Řetězec, který se má odebrat z objektu StringCollection. Hodnota může být null.

Příklady

Následující příklad kódu odebere elementy z objektu 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:
'

Poznámky

Duplicitní řetězce jsou povoleny v nástroji StringCollection. Odebere se pouze první výskyt. Chcete-li odebrat všechny výskyty zadaného řetězce, použijte RemoveAt(IndexOf(value)) opakovaně IndexOf a nevrací -1.

Pokud objekt StringCollection neobsahuje zadaný objekt, StringCollection zůstane beze změny. Žádná výjimka se nevyvolá.

V kolekcích souvislých prvků, tj. například v seznamech, se prvky následující po odebraném prvku posouvají nahoru tak, aby obsadily uvolněné místo. Je-li kolekce indexována, budou aktualizovány rovněž indexy přesunutých prvků. Toto chování se nevztahuje na kolekce, kde prvky jsou koncepčně seskupeny do sad, tj. například na tabulky hash.

Tato metoda určuje rovnost voláním Object.Equalsmetody . Při porovnávání řetězců se rozlišují malá a velká písmena.

Tato metoda provádí lineární vyhledávání; proto je tato metoda operací O(n), kde n je Count.

Platí pro

Viz také