如何将字符串分割成一本字典 [英] How to split string into a dictionary
本文介绍了如何将字符串分割成一本字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个字符串
字符串SX =(colorIndex = 3)(font.family =黑体)(字体。大胆= 1);
和我与
分裂它
的String [] SS = sx.Split(新的char [] {'(',')'}
StringSplitOptions.RemoveEmptyEntries);
相反的,我怎么可能结果分成词典<字符串,串>
?在
造成的字典应该是这样的:
核心价值
colorIndex 3
font.family黑体
font.bold 1
解决方案
有可能更有效的办法,但这应该工作:
字符串SX =(colorIndex = 3)(font.family = Helvicta) (font.bold = 1);
VAR项目= sx.Split(新[] {'(',')'},StringSplitOptions.RemoveEmptyEntries)
。选择(S = GT; s.Split(新[] {'='}));
&字典LT;字符串,字符串>字典=新词典<字符串,字符串>();
的foreach(在项目VAR项目)
{
dict.Add(项[0],项目[1]);
}
I have this string
string sx="(colorIndex=3)(font.family=Helvetica)(font.bold=1)";
and am splitting it with
string [] ss=sx.Split(new char[] { '(', ')' },
StringSplitOptions.RemoveEmptyEntries);
Instead of that, how could I split the result into a Dictionary<string,string>
? The
resulting dictionary should look like:
Key Value
colorIndex 3
font.family Helvetica
font.bold 1
解决方案
There may be more efficient ways, but this should work:
string sx = "(colorIndex=3)(font.family=Helvicta)(font.bold=1)";
var items = sx.Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Split(new[] { '=' }));
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (var item in items)
{
dict.Add(item[0], item[1]);
}
这篇关于如何将字符串分割成一本字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文