我需要根据一组单词从字符串中删除单词:
我要删除的词:
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/