首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过资源文件(txt)将数据保存在SortedDictionary中

通过资源文件(txt)将数据保存在SortedDictionary中
EN

Stack Overflow用户
提问于 2014-08-28 11:42:37
回答 3查看 179关注 0票数 1

我在一个资源文件(*.resx)中保存了一个txt文件,其中包含了一些信息:

代码语言:javascript
复制
    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中:

代码语言:javascript
复制
    23,TRUNK-1  
    24,TRUNK-2  
    25,LEAF-1
    26,LEAF-2
    136,UDPLite

代码语言:javascript
复制
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(proNumber, proName);
}

我怎么能这么做?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-08-28 11:52:35

代码语言:javascript
复制
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]);
}
票数 1
EN

Stack Overflow用户

发布于 2014-08-28 11:50:16

你可以这样做:

代码语言:javascript
复制
        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);
        }
票数 1
EN

Stack Overflow用户

发布于 2014-08-28 11:50:54

似乎你已经做了几乎所有的事情:

代码语言:javascript
复制
foreach (var i in rows)
{
    String[] words = i.Split(new[] { ',' });

    UInt16 proNumber= UInt16.Parse(words[0]);
    string  proName=words[1];

    xTypes.Add(proNumber, proName);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25547955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档