C#: Converting List of Chars to String

asked6 years ago
last updated6 years ago
viewed34.2k times
Up Vote14Down Vote

I'm solving a coding challenge on Coderbyte with C# using lists. I have the desired outcome but need to return it as a string.

I would like to know how to convert my list of chars into a string. Thank you in advance.

Here's my code:

string s = "I love dogs";
int i, j = 0;
List<char> array1 = new List<char>();
List<char> array2 = new List<char>();

for (i = 0; i < s.Length; i++)
{
    if (s.Length == j)
        break;

    if (Char.IsLetter(s[i]))
    {
        array1.Add(s[i]);
    }
    else
    {
        for (j = i; j < s.Length; j++)
        {
            if (Char.IsLetter(s[j]))
            {
                array2.Add(s[i]);
            }
            if (!Char.IsLetter(s[j]) || j == s.Length - 1)
            {
                if (array1.Count >= array2.Count)
                {
                    array2.Clear();
                }
                else
                {
                    array1.Clear();
                    array1.AddRange(array2);
                    array2.Clear();
                }
            }
        }
    }
} // How to convert array1 into String ?