0

私は次のような文字列の配列を持っています:

[0]ボード1
[1]送信されたメッセージ75877814
[2]ISRカウント682900312
[3]バスエラー0
[4]データエラー0
[5]受信タイムアウト0
[6]TXQオーバーフロー0
[7]ハンドラーの失敗なし0
[8]ドライバーの失敗0
[9]スプリアスISR0

角括弧内の数字を明確にするために、配列内の文字列の位置を示しています

文字列の配列を辞書に変換したいのですが、たとえば、各数値の左側にある文字列がキーとして機能します(ISR Count、682900312)

次に、辞書の特定のエントリをVisual Studioのテキストボックス/テーブルに出力します(どちらか良い方)。数字を左揃えにすることをお勧めします。

私の素朴さを失礼します、私は初心者です!

4

4 に答える 4

4

ものすごく単純。試してテストしました

string[] arr = new string[] { "Board1", "ISR Count682900312", ... };

var numAlpha = new Regex("(?<Alpha>[a-zA-Z ]*)(?<Numeric>[0-9]*)");

var res = arr.ToDictionary(x => numAlpha.Match(x).Groups["Alpha"], 
                           x => numAlpha.Match(x).Groups["Numeric"]);

ここに画像の説明を入力してください

于 2012-09-11T11:36:14.497 に答える
1

string [] string = {"Board1"、 "Messages232"};

        Dictionary<string, int> dictionary = new Dictionary<string, int>();

        foreach (var s in strings)
        {
            int index = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (Char.IsDigit(s[i]))
                {
                    index = i;
                    break;
                }
            }
            dictionary.Add(s.Substring(0, index), int.Parse(s.Substring(index)));
        }
于 2012-09-11T11:27:25.263 に答える
1
    var stringArray = new[]
                          {
                              "[0]Board1",
                              "[1]Messages Transmitted75877814",
                              "[2]ISR Count682900312",
                              "[3]Bus Errors0",
                              "[4]Data Errors0",
                              "[5]Receive Timeouts0",
                              "[6]TX Q Overflows0",
                              "[7]No Handler Failures0",
                              "[8]Driver Failures0",
                              "[9]Spurious ISRs0"
                          };


    var resultDict = stringArray.Select(s => s.Substring(3))
        .ToDictionary(s =>
                      {
                          int i = s.IndexOfAny("0123456789".ToCharArray());
                          return s.Substring(0, i);
                      },
                      s =>
                      {
                          int i = s.IndexOfAny("0123456789".ToCharArray());
                          return int.Parse(s.Substring(i));
                      });

編集:括弧内の数字が文字列に含まれていない場合は、を削除し.Select(s => s.Substring(3))ます。

于 2012-09-11T11:35:34.003 に答える
0

どうぞ:

string[] strA = new string[10]
{
    "Board1",
    "Messages Transmitted75877814",
    "ISR Count682900312",
    "Bus Errors0",
    "Data Errors0",
    "Receive Timeouts0",
    "TX Q Overflows0",
    "No Handler Failures0",
    "Driver Failures0",
    "Spurious ISRs0"
};
Dictionary<string, int> list = new Dictionary<string, int>();

foreach (var item in strA)
{
    // this Regex matches any digit one or more times so it picks
    // up all of the digits on the end of the string
    var match = Regex.Match(item, @"\d+");

    // this code will substring out the first part and parse the second as an int
    list.Add(item.Substring(0, match.Index), int.Parse(match.Value));
}
于 2012-09-11T11:35:47.060 に答える