私は持っている
string input = "XXXX-NNNN-A/N";
string[] separators = { "-", "/" };
文字列内の区切り記号の出現位置を調べる必要があります。
出力は次のようになります。
5 "-"
10 "-"
12 "/"
C# で行うには?
私は持っている
string input = "XXXX-NNNN-A/N";
string[] separators = { "-", "/" };
文字列内の区切り記号の出現位置を調べる必要があります。
出力は次のようになります。
5 "-"
10 "-"
12 "/"
C# で行うには?
List<int> FindThem(string theInput)
{
List<int> theList = new List<int>();
int i = 0;
while (i < theInput.Length)
if (theInput.IndexOfAny(new[] { '-', '/' }, i) >= 0)
{
theList.Add(theInput.IndexOfAny(new[] { '-', '/' }, i) + 1);
i = theList.Last();
}
else break;
return theList;
}
メソッドからゼロベースの位置インデックスを取得できますString.IndexOf()
。
これを試して:
int index = 0; // Starting at first character
char[] separators = "-/".ToCharArray();
while (index < input.Length) {
index = input.IndexOfAny(separators, index); // Find next separator
if (index < 0) break;
Debug.WriteLine((index+1).ToString() + ": " + input[index]);
index++;
}