परिचय
StringCollection क्लास स्ट्रिंग्स का एक संग्रह दर्शाता है। IndexOf(string) मेथड किसी स्ट्रिंग की पहली घटना का शून्य-आधारित इंडेक्स लौटाता है; यदि नहीं मिला तो -1 लौटाता है।
उदाहरण 1: Insert और IndexOf
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Accessories");
strCol.Add("Books");
strCol.Add("Electronics");
strCol.Add("Books");
Console.WriteLine("StringCollection तत्व...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
Console.WriteLine("StringCollection तत्व...अपडेटेड");
foreach (string res in strCol) {
Console.WriteLine(res);
}
Console.WriteLine("Books की पहली घटना का इंडेक्स? = " + strCol.IndexOf("Books"));
}
}
आउटपुट
StringCollection तत्व... Accessories Books Electronics Books StringCollection तत्व...अपडेटेड Accessories Books Headphone Electronics Books Books की पहली घटना का इंडेक्स? = 1
उदाहरण 2: सरणी से AddRange और IndexOf
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "D", "H" };
Console.WriteLine("StringCollection तत्व...");
foreach (string str in strArr) {
Console.WriteLine(str);
}
strCol.AddRange(strArr);
Console.WriteLine("5वें इंडेक्स पर तत्व = " + strCol[5]);
Console.WriteLine("StringCollection में स्ट्रिंग्स की संख्या = " + strCol.Count);
Console.WriteLine("D की पहली घटना का इंडेक्स? = " + strCol.IndexOf("D"));
}
}
आउटपुट
StringCollection तत्व... A B C D E F D H 5वें इंडेक्स पर तत्व = F StringCollection में स्ट्रिंग्स की संख्या = 8 D की पहली घटना का इंडेक्स? = 3
मुख्य बिंदु
IndexOfपहली मिलान वाली घटना का इंडेक्स लौटाता है।Insertनिर्दिष्ट इंडेक्स पर तत्व डालता है।AddRangeसरणी से एकाधिक स्ट्रिंग्स जोड़ता है।