📌  相关文章
📜  C#|从StringCollection中删除第一个匹配项

📅  最后修改于: 2021-05-29 19:19:38             🧑  作者: Mango

StringCollection类是.NET Framework类库的新添加,它表示字符串的集合。 StringCollection类在System.Collections.Specialized命名空间中定义。
StringCollection.Remove(String)方法用于从StringCollection中删除特定字符串的首次出现。

句法:

public void Remove (string value);

这里,是要被从StringCollection去除的字符串。该值可以为空。

笔记:

  • StringCollection中允许使用重复的字符串。
  • 仅删除第一个匹配项。若要删除所有出现的指定字符串,请重复使用RemoveAt(IndexOf(value)) ,而IndexOf不返回-1。
  • 如果StringCollection不包含指定的对象,则StringCollection保持不变。没有异常被抛出。
  • 该方法执行线性搜索;因此,此方法是O(n)运算,其中n是Count。

例子:

// C# code to remove the first
// occurrence of a specific string
// from the StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] {"A", "A", "A", "B", "C",
                                           "C", "D", "D", "E"};
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        Console.WriteLine("Elements in StringCollection are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing the first occurrence of
        // "A" from the StringCollection
        myCol.Remove("A");
  
        Console.WriteLine("Elements in StringCollection are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing the first occurrence of
        // "C" from the StringCollection
        myCol.Remove("C");
  
        Console.WriteLine("Elements in StringCollection are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing the first occurrence of
        // "A" from the StringCollection
        myCol.Remove("A");
  
        Console.WriteLine("Elements in StringCollection are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing the first occurrence of
        // "Z" from the StringCollection
        // It would not throw exception even
        // if "Z" does not exist in myCol
        myCol.Remove("Z");
  
        Console.WriteLine("Elements in StringCollection are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
    }
}
输出:
Elements in StringCollection are : 
A
A
A
B
C
C
D
D
E
Elements in StringCollection are : 
A
A
B
C
C
D
D
E
Elements in StringCollection are : 
A
A
B
C
D
D
E
Elements in StringCollection are : 
A
B
C
D
D
E
Elements in StringCollection are : 
A
B
C
D
D
E

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringcollection.remove?view=netframework-4.7.2