Cear Cear - 1 month ago 8
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

Try using Linq:

  string test = "hello";

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

Test:

  // h, he, hel, hell, hello
  Console.Write(string.Join(", ", testing));