user20465780
user20465780

Reputation: 35

How to delete string [] arrays from another string?

First I define my string array and a string from which I want to remove words:

string[] excludeWords = {"word1","word2","word3","word4","word5"}
   
    string sentence = "word1 word2 word5 marble1";

    TextBox1.Text = sentence;

I want to remove excludeWords from sentence so I want this output in my textbox(ASP.NET):

marble1

I tried Except method but not removes the words:

string filteredText = sentence.Except(excludeWords);

TextBox1.Text = filteredText

What method should I use?

Upvotes: 0

Views: 80

Answers (1)

Hossein Sabziani
Hossein Sabziani

Reputation: 3495

var result = excludeWords.Except(sentence.Split(" ")).ToArray();
TextBox1.Text = string.Join(" ", result);

result:

"word3 word4"

enter image description here

You edited the question. In this case, you can use the following code

        string[] excludeWords = { "word1", "word2", "word3", "word4", "word5" };
        string sentence = "word1 word2 word5 marble1";
        var result = sentence.Split(" ").Except(excludeWords).ToArray();
        textBox1.Text= string.Join(" ", result);

result:

marble1

Upvotes: 1

Related Questions