2015-06-21 54 views
1

我有一個字符串數組即如何檢查開始/結束字,長空格分隔字符串

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禁用詞「(whiteSpace),但問題是,我在stopwords數組中使用」i「,並且文本中包含」i「,意味着在」i「開始處沒有空白。所以問題是文本中的開始和結束字符串與stopWords不匹配,所以無法刪除這些字詞。我正在使用的循環是...

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], " "); 
    } 
    } 

任何建議將不勝感激。

+0

一種方法是使用''「'分隔'input_Text'作爲分隔符並對每個元素(=每個單詞)運行測試。您的停用詞列表不需要前導空格和尾隨空格。最後,重新組合成1個字符串,每個字符串之間有空格。 –

回答

0

鑑於你的數據,你可以Replace前添加一個空格字符的開始和結束input_Text,之後將其刪除:

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

效率不高,但應該能正常運行。

相關問題