How to extract words from a string in C# (Split string)

This is incredibly easy in C#. First of all, you need to have a string from which you want to extract words. A word is a string separated by spaces. To do that, just say:

string s = "Welcome to my blog!";

Now, when I use the Split command, it will return an array of individual strings. So, the implementation will be:

string[] words = s.Split(" ",StringSplitOptions.RemoveEmptyEntries);

As a result, words array will contain “Welcome”, “to”, “my”, “blog!”.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.