StringCollection.RemoveAt Method
Removes the string at the specified index of the StringCollection.
[Visual Basic] Public Overridable Sub RemoveAt( _ ByVal index As Integer _ ) Implements IList.RemoveAt [C#] public virtual void RemoveAt( int index ); [C++] public: virtual void RemoveAt( int index ); [JScript] public function RemoveAt( index : int );
Parameters
- index
- The zero-based index of the string to remove.
Implements
Exceptions
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | index is less than zero.
-or- index is equal to or greater than Count. |
Remarks
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.
Example
[Visual Basic, C#, C++] The following code example removes elements from the StringCollection.
[Visual Basic] Imports System 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 'Main Public Shared Sub PrintValues(myCol As IEnumerable) Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator() While myEnumerator.MoveNext() Console.WriteLine(" {0}", myEnumerator.Current) End While Console.WriteLine() End Sub 'PrintValues End Class 'SamplesStringCollection '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: ' [C#] 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 ) { System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( " {0}", myEnumerator.Current ); 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: */ [C++] #using <mscorlib.dll> #using <System.dll> using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void PrintValues(IEnumerable* myCol) { System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator(); while (myEnumerator->MoveNext()) Console::WriteLine(S" {0}", myEnumerator->Current); Console::WriteLine(); } int main() { // Creates and initializes a new StringCollection. StringCollection* myCol = new StringCollection(); String* myArr[] = { S"RED", S"orange", S"yellow", S"RED", S"green", S"blue", S"RED", S"indigo", S"violet", S"RED" }; myCol->AddRange(myArr); Console::WriteLine(S"Initial contents of the StringCollection:"); PrintValues(myCol); // Removes one element from the StringCollection. myCol->Remove(S"yellow"); Console::WriteLine(S"After removing \"yellow\":"); PrintValues(myCol); // Removes all occurrences of a value from the StringCollection. int i = myCol->IndexOf(S"RED"); while (i > -1) { myCol->RemoveAt(i); i = myCol->IndexOf(S"RED"); } Console::WriteLine(S"After removing all occurrences of \"RED\":"); PrintValues(myCol); // Clears the entire collection. myCol->Clear(); Console::WriteLine(S"After clearing the collection:"); PrintValues(myCol); } /* 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: */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
StringCollection Class | StringCollection Members | System.Collections.Specialized Namespace