Convert string to array of lines & line array to string in C#


See below example to convert text of array of lines in c# programming. Just split string with new line character after checking null and empty character.

public static string[] ToStringArray(string text)
{
    if (String.IsNullOrEmpty(text) == false)
        return text.Split(new string[] { Environment.NewLine },
            StringSplitOptions.None);
    else
    {
        return new string[0];
    }
}

Below is example to make string from array of lines.

public static string ToText(string[] lines)
{
    if (lines != null && lines.Length > 0)
    {
        var sb = new StringBuilder(lines.Length * 200);
        for (int i = 0; i < lines.Length; i++ )
        {
            if (i < lines.Length - 1)
                sb.AppendLine(lines[i]);
            else
            {
                sb.Append(lines[i]);
            }
        }
        return sb.ToString();
    }
    else
    {
        return string.Empty;
    }
}