0

我有一个包含各种字符串记录的列表。一些记录由分号分隔的各种子记录组成。例如如下

生活技能

没有相关主题

沟通

职业; 听力技巧; 个人发展; 提问技巧;辅导/指导;认出; 招聘与选拔。

客户服务

体育

我现在要做的是遍历记录,将所有包含分号的记录分开,确保没有重复

for(int i=0; i<lst.Count; i++) {
    // seperate the records that contains ';' into individual unique items
}

我怎样才能做到这一点?

4

3 回答 3

3
        List<String> lst = new List<string>();
        lst.Add("Life Skills");
        lst.Add("Life Skills");
        lst.Add("Communication");
        lst.Add("Careers; Listening Skills;Life Skills; Personal Development; Questioning Skills; Coaching/Mentoring; Recognition; Recruitment and Selection.");
        lst.Add("No Related Topics");

        List<string> newList = new List<string>();

        foreach (string str in lst)
        {
            var temp = str.Split(';');
            if (temp.Length > 1)
            {
                for (int i = 0; i < temp.Length; i++)
                {
                    if (!newList.Contains(temp[i]))
                    {
                        newList.Add(temp[i]);
                    }
                }
            }
            else
            {
                if (!newList.Contains(str))
                {
                    newList.Add(str);
                }
            }
        }
于 2013-02-08T12:21:57.103 回答
1

你可以使用 Linq 来实现它

lst = lst
  .SelectMany(i => string.Split(";", i))
  .Select(i => i.Trim())
  .Distinct()
  .ToList();
于 2013-02-08T12:15:39.110 回答
0

您想为您的程序实现某种解析器。StackOverflow 不会为您编写程序,但我建议您查看http://boost-spirit.com/home/

如果您选择自己滚动,可能出于许可原因,加载您的文本输入,可能将结果加载到缓冲区中直到到达分号,然后将缓冲区复制到字符串并将其推送到数组中将是理想的。从那里您可以继续循环,直到到达文件末尾。

于 2013-02-08T12:13:31.447 回答