C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#のStringCollectionで最初に出現するインデックス?


StringCollectionで最初に出現するインデックスを取得するには、コードは次のとおりです-

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 elements...");
      foreach (string res in strCol) {
         Console.WriteLine(res);
      }
      strCol.Insert(2, "Headphone");
      Console.WriteLine("StringCollection elements...UPDATED");
      foreach (string res in strCol) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Index of first occurrence of Books? = "+strCol.IndexOf("Books"));
   }
}

出力

これにより、次の出力が生成されます-

StringCollection elements...
Accessories
Books
Electronics
Books
StringCollection elements...UPDATED
Accessories
Books
Headphone
Electronics
Books
Index of first occurrence of Books? = 1

別の例を見てみましょう-

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 elements...");
      foreach (string str in strArr) {
         Console.WriteLine(str);
      }
      strCol.AddRange(strArr);
      Console.WriteLine("Element at 5th index = "+strCol[5]);
      Console.WriteLine("Count of strings in StringCollection = " + strCol.Count);
      Console.WriteLine("Index of first occurrence of D? = "+strCol.IndexOf("D"));
   }
}

出力

これにより、次の出力が生成されます-

StringCollection elements...
A
B
C
D
E
F
D
H
Element at 5th index = F
Count of strings in StringCollection = 8
Index of first occurrence of D? = 3

  1. Pythonでの真の数の最初の出現

    この記事では、指定された数値のリストで最初に発生するゼロ以外の数値を見つける必要があります。 列挙して次へ 列挙してすべての要素のリストを取得し、次の関数を適用して最初の非ゼロ要素を取得します。 例 listA = [0,0,13,4,17] # Given list print("Given list:\n " ,listA) # using enumerate res = next((i for i, j in enumerate(listA) if j), None) # printing result print("The first non zero

  2. RediSearch2.0が最初のマイルストーンに到達

    RediSearch2.0の開発における最初のマイルストーンのリリースを発表できることを嬉しく思います。 RediSearchは、Redisデータをクエリして、さまざまな複雑な質問に答えることができるリアルタイム検索エンジンです。 2.0-M01と呼ばれるこのマイルストーンは、インデックスがデータと同期し続ける方法の再構築を示しています。インデックスを介してデータを書き込む代わりに( FT.ADD を使用) コマンド)、RediSearchはハッシュで書き込まれたデータを追跡し、自動的にインデックスを作成します。 ここでの大きな利点は、アプリケーションコードを更新しなくても、既存のRe