C# में StringCollection वर्ग की Remove(string) विधि का उपयोग करके निर्दिष्ट स्ट्रिंग की पहली घटना को हटाया जा सकता है। यह विधि संग्रह में मिलने वाली पहली मिलान वाली स्ट्रिंग को हटाती है और सफल होने पर true लौटाती है।
उदाहरण 1: सरणी से StringCollection बनाकर तत्व हटाना
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "100", "400", "500" };
Console.WriteLine("सरणी तत्व...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("कुल तत्वों की संख्या = " + stringCol.Count);
stringCol.Remove("100");
Console.WriteLine("कुल तत्वों की संख्या अब = " + stringCol.Count);
}
}
आउटपुट
सरणी तत्व... 100 200 100 400 500 कुल तत्वों की संख्या = 5 कुल तत्वों की संख्या अब = 4
उदाहरण 2: एकाधिक स्ट्रिंग्स हटाना
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "10", "20", "30", "40", "50", "60", "70", "80" };
Console.WriteLine("सरणी तत्व...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("कुल तत्वों की संख्या = " + stringCol.Count);
stringCol.Remove("50");
stringCol.Remove("60");
stringCol.Remove("70");
Console.WriteLine("कुल तत्वों की संख्या अब = " + stringCol.Count);
}
}
आउटपुट
सरणी तत्व... 10 20 30 40 50 60 70 80 कुल तत्वों की संख्या = 8 कुल तत्वों की संख्या अब = 5
मुख्य बिंदु
Remove(string)पहली मिलान वाली स्ट्रिंग हटाता है- वापसी मान
boolहोता है: सफलता परtrue AddRange()से सरणी के सभी तत्व एक साथ जोड़ सकते हैंRemoveAt(index)से सूचकांक द्वारा हटा सकते हैंStringCollectionकेवल स्ट्रिंग संग्रहीत करता है