如何检查长 space 分隔字符串中的 starting/ending 单词

how to check starting/ending word in a long space delimited string

我有一个字符串数组,即

string[] input_Text = new string[] { "i am wanting to take admission in the univeristy of the islamabad", "we are enjoying all the talent here at the city of atlanta", "what are you doing there" };

和一个停用词数组,即

string[] stopWords = new string[] { " are ", " am ", " all ", " at ", " here ", " i ", " in ", " of ", " take ", " the ", " there ", " to ", " what ", " we ", " you " }; 

我必须用“”(空白)替换 input_Text 中的停用词,但问题是我在停用词数组中有“ i ”并且文本在开始时包含 'i' 意味着没有空白在 'i' 的开头。所以问题是文本中的开始和结束词串与停用词不匹配,因此无法删除这些词。我使用的循环是....

for (int i = 0; i < input_Text.Count(); i++) 
 { 
   for (int j = 0; j < stopWords.Count(); j++) 
   {
      input_Text[i] = input_Text[i].Replace(stopWords[j], " ");
   } 
  }

如有任何建议,我们将不胜感激。

根据您的数据,您可以在 Replace 之前的 input_Text 的开头和结尾添加一个 space 字符,然后将其删除:

string s = " " + input_Text[i] + " ";
s = s.Replace(stopWords[j], " ");
input_Text[i] = s.Substring(1, s.Length - 2);

效率不是很高,但应该可以正常工作。