I have a List in below structure:
Tuple<string, string>
like this:
And I would like to join the list to form a string like below:
['A', '1'],['B', '2'], ['C', '3']...
And I am now using below code to do:
string result = "";
for (int i = 0; i < list.Count; i++)
{
result += "[ '" + list[i].Item1 + "', '" + list[i].Item2 + "'],";
}
The code works OK, but would like to ask if there're any better ways to do that?
You can make it more compact by using Linq, string.Join
and string.Format
:
result = string.Join(",", list.Select(t => string.Format("[ '{0}', '{1}']", t.Item1, t.Item2)));
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