2010-09-03 49 views
4

將字符串轉換爲C#中的單詞列表的最有效方法是什麼?C# - 關鍵字的字符串

例如:

Hello... world 1, this is amazing3,really , amazing! *bla* 

應該變成串名單如下:

["Hello", "world", "1", "this", "is", "amazing3", "really", "amazing", "bla"] 

注意,它應該支持英語以外的其他語言。

我需要這個,因爲我想收集特定文本中的關鍵字列表。

謝謝。

+0

您期待標記一個字符串。看看James的回答。 – Gage 2010-09-03 19:04:44

+0

你需要處理多少文字?幾千字或數百萬? – Larsenal 2010-09-03 19:16:35

+0

其實最大值可能會像100個字。 – 2010-09-03 20:44:32

回答

5

如何使用正則表達式?你可以使表達任意複雜,但我在這裏應該適用於大多數輸入。

new RegEx(@"\b(\w)+\b").Matches(text); 
+0

這是完全**我需要的。謝謝! – 2010-09-03 20:46:42

5
char[] separators = new char[]{' ', ',', '!', '*', '.'}; // add more if needed 

string str = "Hello... world 1, this is amazing3,really , amazing! *bla*"; 
string[] words= str.Split(separators, StringSplitOptions.RemoveEmptyEntries); 
+0

以及@#$%^&()_ = ....等其他標誌呢?我需要一個通用的方式。 – 2010-09-03 19:03:33

+2

@Alon,將它們添加到分隔符列表中。 – Gage 2010-09-03 19:09:06