简体   繁体   English

从列表项中删除分号

[英]Remove semicolons from list items

I have a list that contans various string records. 我有一个包含各种字符串记录的列表。 Some of the records consists of various sub-records that are seperated by semicolons. 一些记录由分号分隔的各种子记录组成。 For example as follows 例如如下

Life Skills 生活技能

No Related Topics 没有相关主题

Communication 通讯

Careers; 职业; Listening Skills; 听力技巧; Personal Development; 个人发展; Questioning Skills; 提问技巧; Coaching/Mentoring; 教练/辅导; Recognition; 承认; Recruitment and Selection. 招聘与选拔。

Customer Service 客户服务

Physical Education 体育

What I want to do now is to iterate through the records seperate all the records that contains the semicolon and make sure there are no duplicates . 我现在想做的是遍历记录,将包含分号的所有记录分开,确保没有重复

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

How can I do this? 我怎样才能做到这一点?

        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);
                }
            }
        }

You can use Linq to implement it 您可以使用Linq来实现它

lst = lst
  .SelectMany(i => string.Split(";", i))
  .Select(i => i.Trim())
  .Distinct()
  .ToList();

You want to implement some kind of parser for your program. 您想为程序实现某种解析器。 StackOverflow isn't going to write your programs for you but I suggest looking into http://boost-spirit.com/home/ StackOverflow不会为您编写程序,但是我建议您浏览http://boost-spirit.com/home/

If you choose to roll your own, perhaps for licensing reasons, loading your text input, perhaps loading the result into a buffer until a semicolon is reached, then copying the buffer to a string and pushing it into an array would be ideal. 如果出于许可原因选择滚动自己,则加载文本输入,或者将结果加载到缓冲区中,直到达到分号为止,然后将缓冲区复制到字符串中并将其推入数组将是理想的选择。 From there you could keep looping until you reach the end of the file. 从那里您可以继续循环播放,直到到达文件末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM