我想忽略標點符號。因此,我試圖編寫一個程序來計算我的文本中每個單詞的所有出現,但沒有考慮標點符號。 所以我的計劃是:如何忽略標點符號c#
static void Main(string[] args)
{
string text = "This my world. World, world,THIS WORLD ! Is this - the world .";
IDictionary<string, int> wordsCount =
new SortedDictionary<string, int>();
text=text.ToLower();
text = text.replaceAll("[^0-9a-zA-Z\text]", "X");
string[] words = text.Split(' ',',','-','!','.');
foreach (string word in words)
{
int count = 1;
if (wordsCount.ContainsKey(word))
count = wordsCount[word] + 1;
wordsCount[word] = count;
}
var items = from pair in wordsCount
orderby pair.Value ascending
select pair;
foreach (var p in items)
{
Console.WriteLine("{0} -> {1}", p.Key, p.Value);
}
}
輸出是:
is->1
my->1
the->1
this->3
world->5
(here is nothing) -> 8
我怎麼可以在這裏刪除標點?
使用'text.Split(新[] {」 「」 「,」 - 「,」!「,」。「},StringSplitOptions.RemoveEmptyEntries);'排除空的條目。 – Kvam