2009-06-30 123 views

回答

4

注意除非您使用Regex word boundaries,否則您可能會與Scunthorpe (Sfannythorpe) problem發生衝突。

string pattern = @"\band\b"; 
Regex re = new Regex(pattern); 

string input = "a band loves and its fans"; 

string output = re.Replace(input, ""); // a band loves its fans 

注意'帶'中的'和'是未觸及的。

0

可以匹配的話,並使用正則表達式刪除它們。

+0

一個示例或示例的鏈接可能會更有幫助 – jao 2009-06-30 08:54:41

3

您確實可以取代你使用.Replace功能(如colithium描述)的單詞列表...

myString.Replace("and", "") 

編輯:

......但事實上,更好的方法是使用正則表達式(如edg建議)以避免替換部分單詞。


至於你的問題建議你想清理一句話讓meaningfull的話,你所要做的不僅僅是刪除兩個和三個字母的單詞。

你需要的是停止的話列表:停止字的英語可以在這裏找到 http://en.wikipedia.org/wiki/Stop_word

一個逗號分隔列表: http://www.textfixer.com/resources/common-english-words.txt

+1

很好的答案但我會用正則表達式代替String.Replace – 2009-06-30 16:07:08

+0

我同意...我已經更新了我的答案 – WowtaH 2009-06-30 17:09:56

相關問題