How can I remove duplicates from a TextBox?

喜你入骨 提交于 2019-12-10 16:09:55

问题


I have a text box that has each item on a new line. I am trying to remove duplicates from this textBox. I can't think of anything. I tried adding each item to an array and the removing the duplicates, but it doesn't work. Are there any other options?


回答1:


yourTextBox.Text = string.Join(Environment.NewLine, yourArray.Distinct());



回答2:


Building on what Anthony Pegram wrote, but without needing a separate array:

yourTextBox.Text = string.Join(Environment.NewLine, yourTextBox.Lines.Distinct());




回答3:


Add all the Items to string array and use this code to remove duplicates

public static string[] RemoveDuplicates(string[] s)
{
    HashSet<string> set = new HashSet<string>(s);
    string[] result = new string[set.Count];
    set.CopyTo(result);
    return result;
}

For more information have a look at Remove duplicates from array



来源:https://stackoverflow.com/questions/4572987/how-can-i-remove-duplicates-from-a-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!