問題描述
我需要從字符串中刪除重復(fù)的單詞.我該怎么做呢?
I need to remove duplicate words from a string. How would I go about doing that?
推薦答案
如果要去掉重復(fù)"這個詞:
If you want to remove the word "duplicates":
string duplicatesRemoved = RTBstring.Replace("duplicates", "");
;)
刪除重復(fù)單詞的簡單(且過于簡單)方法是拆分空格字符并使用 LINQ 的 Distinct() 方法:
The easy (and overly simplistic) way to remove duplicate words is to split on the space character and use LINQ's Distinct() method:
string duplicatesRemoved = string.Join(" ", RTBstring.Split(' ').Distinct());
但是,如果您使用的是實際句子(即標(biāo)點符號會破壞它),這將不會有用.如果沒有明確定義重復(fù)的含義以及預(yù)期的輸入是什么,就很難給出準(zhǔn)確的答案.
But this won't work in a useful way if you're working with actual sentences (i.e. punctuation will break it). Without a clear definition of what you mean by duplicates and what the expected input is, it's hard to give an accurate answer.
這篇關(guān)于從字符串中刪除重復(fù)的單詞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!