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 ?
One option for this is to use the string constructor:
var myString = new string(array1.ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With