C#-এ StringCollection ক্লাসে নির্দিষ্ট স্ট্রিং অবজেক্টের সূচক (index) পেতে 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("নির্দিষ্ট অবজেক্ট 'Electronics'-এর সূচক = " + strCol.IndexOf("Electronics"));
}
}
আউটপুট
StringCollection উপাদানগুলো... Accessories Books Electronics Books StringCollection উপাদানগুলো... আপডেটেড Accessories Books Headphone Electronics Books নির্দিষ্ট অবজেক্ট 'Electronics'-এর সূচক = 3
উদাহরণ ২
অন্য একটি উদাহরণ:
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "300", "400", "500" };
Console.WriteLine("Array উপাদানগুলো...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("নির্দিষ্ট স্ট্রিংটি StringCollection-এ আছে কি? = " + stringCol.Contains("800"));
Console.WriteLine("মোট উপাদানের সংখ্যা = " + stringCol.Count);
Console.WriteLine("StringCollection দিয়ে পুনরাবৃত্তি = " + stringCol.Count);
StringEnumerator myenum = stringCol.GetEnumerator();
while (myenum.MoveNext())
Console.WriteLine(myenum.Current);
Console.WriteLine("নির্দিষ্ট অবজেক্ট '500'-এর সূচক = " + stringCol.IndexOf("500"));
Console.WriteLine("নির্দিষ্ট অবজেক্ট '1000'-এর সূচক = " + stringCol.IndexOf("1000"));
}
}
আউটপুট
Array উপাদানগুলো... 100 200 300 400 500 নির্দিষ্ট স্ট্রিংটি StringCollection-এ আছে কি? = False মোট উপাদানের সংখ্যা = 5 StringCollection দিয়ে পুনরাবৃত্তি = 5 100 200 300 400 500 নির্দিষ্ট অবজেক্ট '500'-এর সূচক = 4 নির্দিষ্ট অবজেক্ট '1000'-এর সূচক = -1