.NET 2.0 - Text Splitting Text

Suppose you have output like this:

Word1           Word2   Word3      Word4

      

Where the number of spaces between words is arbitrary. I want to split it into an array of words.

I used the following code:

string[] tokens =
         new List<String>(input.Split(' '))
             .FindAll
             (
                 delegate(string token)
                 {
                      return token != String.Empty;
                 }
             ).ToArray();

      

Not really efficient, but does the job well.

How do you do it?

+1


source to share


2 answers


He already mentions string.Split (). What it is missing is StringSplitOptions.RemoveEmptyEntries:



string[] tokens = input.Split(new char[] { ' ' },
    StringSplitOptions.RemoveEmptyEntries); 

      

+18


source


I would use a regular expression to separate with "\ w +" for the pattern.



+1


source







All Articles