.NET 2.0 - Tokenizing space separated text
-
19-08-2019 - |
Question
Suppose you have output like this:
Word1 Word2 Word3 Word4
Where the number of spaces between words is arbitrary. I want to break 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 exactly efficient, but does the job nicely.
How would you do it?
Solution
He already mentions string.Split(). What he's missing is StringSplitOptions.RemoveEmptyEntries:
string[] tokens = input.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
OTHER TIPS
I would use a regex for the split with "\w+" for the pattern.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow