下面是示例字符串中的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]);发布于 2012-09-19 10:14:47
试试这个:
var result = input.Split('|')
.Select(r => r.Split(',')
.Select(c => c.Split('='))
.ToDictionary(x => x[0], x => x[1]));发布于 2012-09-19 10:19:08
看起来你想从
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])
};
});https://stackoverflow.com/questions/12487613
复制相似问题