2010-09-03 83 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