asp.net 如何从另一个字符串中删除string []数组?

chhkpiq4  于 2022-11-19  发布在  .NET
关注(0)|答案(1)|浏览(147)

首先,我定义字符串数组和一个字符串,从中删除单词:

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

    TextBox1.Text = sentence;

我想从句子中删除excludeWords,所以我想在我文本框(ASP.NET)中输出以下内容:

marble1

我试过Except方法但无法删除这几个字:

string filteredText = sentence.Except(excludeWords);

TextBox1.Text = filteredText

我该用什么方法?

5hcedyr0

5hcedyr01#

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

实验结果:
“第3个字第4个字”

您编辑了问题。在这种情况下,可以使用以下代码

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

实验结果:
大理石1

相关问题