StringCollection.Remove(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从 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 保持不变。 不会引发异常。
在由连续的元素组成的集合(如列表)中,已移除元素下面的元素将上移以占据空出的位置。 如果集合具有索引,则移动的元素的索引也将更新。 此行为不适用于元素按概念划分为不同存储桶的集合,如哈希表。
此方法通过调用 Object.Equals来确定相等性。 字符串比较区分大小写。
此方法执行线性搜索;因此,此方法是 O (n
) 操作,其中 n
为 Count。