Alon Gubkin
Alon Gubkin

Reputation: 57129

C# - string to keywords

What is the most efficient way to turn string to a list of words in C#?

For example:

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

should turn into the following list of strings:

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

Note that it should support other languages other than English.

I need this because I want to collect a list of keywords from specific text.

Thanks.

Upvotes: 4

Views: 193

Answers (3)

Brian Gideon
Brian Gideon

Reputation: 48949

How about using regular expressions? You could make the expression arbitrarily complex, but what I have here should work for most inputs.

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

Upvotes: 5

James Curran
James Curran

Reputation: 103515

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);

Upvotes: 5

Hans Passant
Hans Passant

Reputation: 941515

You need a lexer.

Upvotes: 2

Related Questions