Cear Cear - 1 year ago 56
C# Question

Converting a string to an array with its letters

Basically what I'm trying to do is take a string

string test = "hello";

and then turn it into an array as such:

string[] testing = { "h", "he", "hel", "hell", "hello" };

is this possible?

Answer Source

Try using Linq:

  string test = "hello";

  string[] testing = Enumerable
    .Range(1, test.Length)
    .Select(length => test.Substring(0, length))


  // h, he, hel, hell, hello
  Console.Write(string.Join(", ", testing)); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download