-1

私は持っている

string input = "XXXX-NNNN-A/N";
string[] separators = { "-", "/" };

文字列内の区切り記号の出現位置を調べる必要があります。

出力は次のようになります。

5 "-" 
10 "-"
12 "/"

C# で行うには?

4

6 に答える 6

1
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;
}
于 2013-05-03T13:19:59.157 に答える
1

メソッドからゼロベースの位置インデックスを取得できますString.IndexOf()

于 2013-05-03T13:19:26.353 に答える
0

これを試して:

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++;
}
于 2013-05-03T13:23:34.310 に答える