c# - 使用 C# 从数组中的单词中删除字符串中的单词

标签 c# arrays string

我需要根据一组单词从字符串中删除单词:

我要删除的词:

DE DA DAS DO DOS AN NAS NO NOS EM E A AS O OS AO AOS P LDA AND

如果我收到如下字符串:

编辑:这个字符串已经从任何符号中“清除”

THIS IS AN AMAZING WEBSITE AND LAYOUT

结果应该是:

THIS IS AMAZING WEBSITE LAYOUT

到目前为止我有:

public static string StringWordsRemove(string stringToClean, string wordsToRemove)
{
    string[] splitWords = wordsToRemove.Split(new Char[] { ' ' });

    string pattern = "";

    foreach (string word in splitWords)
    {
        pattern = @"\b" + word + "\b";
        stringToClean = Regex.Replace(stringToClean, pattern, "");
    }

    return stringToClean;
}

但它并没有删除单词,知道吗?

我不知道我使用的是不是最有效的方法,也许把单词放在一个数组中只是为了避免它们一直被拆分?

谢谢

最佳答案

private static List<string> wordsToRemove =
    "DE DA DAS DO DOS AN NAS NO NOS EM E A AS O OS AO AOS P LDA AND".Split(' ').ToList();

public static string StringWordsRemove(string stringToClean)
{
    return string.Join(" ", stringToClean.Split(' ').Except(wordsToRemove));
}

处理标点符号的修改:

public static string StringWordsRemove(string stringToClean)
{
    // Define how to tokenize the input string, i.e. space only or punctuations also
    return string.Join(" ", stringToClean
        .Split(new[] { ' ', ',', '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries)
        .Except(wordsToRemove));
}

关于c# - 使用 C# 从数组中的单词中删除字符串中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17678798/

相关文章:

ios - 快速在 TableView 之间传递数据

java - 将日期字符串转换为整数数组

c++ - 如何将 std::string 传递给 glShaderSource?

java - 摩尔斯电码转换器

c# - 根据 ObservableCollection 中项目的属性使用 CanExecute 创建命令

c# - 在 ListView C# 中拖放

c - 处理二维数组时出现总线错误

c# - C# 应用程序的最大开放端口数

c# - 为什么 C# 不提供类似于 C++ 的常量?

javascript - javascript 中的对象数组未按预期返回