1
List<string> l = new List<string>();
l.Add("This is TEXT");
l.Add("Convert it to words");

How can I convert in string Array?

Like this:

string[] array = new string{"this","is","TEXT","Convert","it","to","words"};
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Nab
  • 61
  • 2
  • 10

2 Answers2

2

Try this:

List<string> listArray = new List<string>();
listArray.Add("This is TEXT");
listArray.Add("Convert it to words");

// Convert

string[] arrayString = String.Join(" ", listArray).Split(' ');

Working Example here..

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

Or use Linq with SelectMany (that operates on an IEnumeration, applies the same thing on each and puts all the result into one IEnumeration as result - see f.e. here: Difference Between Select and SelectMany)

List<string> listArray = new List<string>();
listArray.Add("This is TEXT");
listArray.Add("Convert it to words");

// this is the array holding all words
var result = listArray.SelectMany(el => el.Split(" ".ToCharArray())).ToArray();

// this puts , between any words in result to output it ...
Console.WriteLine(string.Join(",", result));
Console.ReadLine();

DotNetFiddle

Edit: Difference to un-lucky`s solution:

  • He takes each sentence, concatenates them using a space producing an even bigger string. This resulting string then gets split on spaces to form your array.

  • mine uses Linq to split each sentence into theire words and after it, all words are put into one IEnumerable collection, which I enumerate into an array.

Both produce the desired output. The SelectMany is probably "cheaper" because no long string has to be constructed first. One would have to take a look into the implementations though - for your problem the internal differences between both methods are moot to discuss.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69