0

这里有 2 条记录是示例字符串,“|” 表示新记录或行,“,”分隔对,“=”将键与值分开。如果它是单行或记录,但不是多行或在这种情况下为 2 行,则下面的代码将起作用。完成这项工作需要什么才能让我得到 2 行,每行 3 个元素?

string s1 = "colorIndex=3,font.family=Helvicta,font.bold=1|colorIndex=7,font.family=Arial,font.bold=0";
string[] t = s1.Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);

Dictionary<string, string> dictionary =
                 t.ToDictionary(s => s.Split('=')[0], s => s.Split('=')[1]);
4

2 回答 2

2

尝试这个:

var result = input.Split('|')
                  .Select(r => r.Split(',')
                                .Select(c => c.Split('='))
                                .ToDictionary(x => x[0], x => x[1]));
于 2012-09-19T02:14:46.637 回答
0

好像你想开始

class Font {
    public int ColorIndex { get; set; }
    public string FontFamily { get; set; }
    public bool Bold { get; set; }
}

然后:

var fonts = s1.Split('|')
  .Select(s => {
      var fields = s.Split(',');
      return new Font {
          ColorIndex = Int32.Parse(fields[0].Split('=')[0]),
          FontFamily = fields[1].Split('=')[1],
          Bold = (bool)Int32.Parse(fields[2].Split('=')[2])
      };
  });
于 2012-09-19T02:19:07.853 回答