我有这个功能可以从文本中提取所有单词
public static string[] GetSearchWords(string text)
{
string pattern = @"\S+";
Regex re = new Regex(pattern);
MatchCollection matches = re.Matches(text);
string[] words = new string[matches.Count];
for (int i=0; i<matches.Count; i++)
{
words[i] = matches[i].Value;
}
return words;
}
我想从返回数组中排除一个单词列表,单词列表如下所示
string strWordsToExclude="if,you,me,about,more,but,by,can,could,did";
如何修改上述函数以避免返回列表中的单词。
最佳答案
string strWordsToExclude="if,you,me,about,more,but,by,can,could,did";
var ignoredWords = strWordsToExclude.Split(',');
return words.Except(ignoredWords).ToArray();
我认为Except
方法适合您的需求
关于c# - 从文本中提取关键字并排除单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691751/