0

我有一个这样的字符串数组:

[0]板1
[1]发送的消息75877814
[2]ISR 计数682900312
[3]总线错误0
[4]数据错误0
[5]接收超时0
[6]TX Q 溢出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

字符串[] 字符串 = { "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 回答