我在一个资源文件(*.resx)中保存了一个txt文件,其中包含了一些信息:
23,TRUNK-1,Trunk-1,,[Barry_Boehm]
24,TRUNK-2,Trunk-2,,[Barry_Boehm]
25,LEAF-1,Leaf-1,,[Barry_Boehm]
26,LEAF-2,Leaf-2,,[Barry_Boehm]
136,UDPLite,,,[RFC3828] ..。并希望将第一个和第二个条目保存到SortedDictionary中:
23,TRUNK-1
24,TRUNK-2
25,LEAF-1
26,LEAF-2
136,UDPLitepublic static SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();
String[] rows = Regex.Split(Resources.ProTypes.ProTypesSource, "\r\n");
foreach (var i in rows)
{
String[] words = i.Split(new[] { ',' });
...
xTypes.Add(proNumber, proName);
}我怎么能这么做?
发布于 2014-08-28 11:52:35
public static SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();
String[] rows = Regex.Split(Resources.ProTypes.ProTypesSource, "\r\n");
foreach (var i in rows){
String[] words = i.Split(new[] { ',' });
xTypes.Add(UInt16.Parse(words[0]), words[1]);
}发布于 2014-08-28 11:50:16
你可以这样做:
SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();
String[] rows = Regex.Split("23,TRUNK-1,Trunk-1,,[Barry_Boehm]", "\r\n");
foreach (var i in rows)
{
String[] words = i.Split(new[] { ',' });
UInt16 proNumber = Convert.ToUInt16(words[0]);
string proName = words[1];
xTypes.Add(proNumber, proName);
}发布于 2014-08-28 11:50:54
似乎你已经做了几乎所有的事情:
foreach (var i in rows)
{
String[] words = i.Split(new[] { ',' });
UInt16 proNumber= UInt16.Parse(words[0]);
string proName=words[1];
xTypes.Add(proNumber, proName);
}https://stackoverflow.com/questions/25547955
复制相似问题