0

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?

1 Answer 1

1
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

Sign up to request clarification or add additional context in comments.

5 Comments

this does not work; it gives error can not convert string to chart...
@user20465780 I added an image to the answer so you can see the result
@user20465780 Be careful not to put ' instead of ".
sorry apologize I made a mistake I want an output where excludeWords deletes elements what sentence contains if it match; I reedited the post...
@user20465780 I edited the answer for the new state of the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.