开发者

How can I remove duplicates from a TextBox?

开发者 https://www.devze.com 2023-02-02 03:46 出处:网络
I have a text box that has each item on a new line. I am trying to r开发者_JAVA百科emove duplicates from this textBox. I can\'t think of anything. I tried adding each item to an array and the removing

I have a text box that has each item on a new line. I am trying to r开发者_JAVA百科emove 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?


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


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

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消