Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Converting List of Chars to String [duplicate]

Tags:

string

c#

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 ?
like image 864
Bennity Avatar asked Oct 23 '19 13:10

Bennity


1 Answers

One option for this is to use the string constructor:

var myString = new string(array1.ToArray());
like image 175
haldo Avatar answered Oct 17 '22 16:10

haldo