6

I have a List in below structure:

Tuple<string, string>

like this:

enter image description here

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?

User2012384
  • 4,769
  • 16
  • 70
  • 106
  • Depends what you mean by "better". Certainly nothing that performs significantly better, unless you have millions of rows and decide to change to `StringBuilder`. – D Stanley Jan 22 '16 at 04:22
  • Oh, I mean easier to read.. or this is already the simplest way to write..? – User2012384 Jan 22 '16 at 04:23
  • 3
    You could use `string.Format` and/or Linq, but neither of those are _huge_ improvements in my opinion. – D Stanley Jan 22 '16 at 04:25
  • 2
    "Easier to read" is completely opinion based... Mine would be to use `String.Join(list, x=> String.Format("['{0}','{1}']", x.Item1, x.Item2)` to represent your exact code. But since I'm known string *escape* master :) I'd just use JSON serializer to avoid all "what if there is quote in one of the elements" issues. – Alexei Levenkov Jan 22 '16 at 04:27
  • @AlexeiLevenkov I also want to use JSON serializer if possible... but unfortunately it isn't a json string – User2012384 Jan 22 '16 at 04:28

5 Answers5

20

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)));
D Stanley
  • 149,601
  • 11
  • 178
  • 240
9

You can do it using linq and string interpolation like this:

string.Join(", ", list.Select(t => $"['{t.Item1}', '{t.Item2}']"));
Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
2

There are some benchmarking (note: each word contains one link, but one of the best reference I think is this.) on how to join the string.

Quote from the reference:

Rico Mariani, the .NET Performance guru, had an article on this very subject. It's not as simple as one might suspect.

You could take a look on string.Concat and string.Builder in particular, they are usually faster than + operator.

Here I use string.Concat:

string str = string.Concat(new string[] { "[ '", list[i].Item1, "', '", list[i].Item2, "']," });
Community
  • 1
  • 1
Ian
  • 30,182
  • 19
  • 69
  • 107
2

This might also do the trick for you

var input = new List<Tuple<string, string>> 
                { 
                    Tuple.Create("A", "1"), 
                    Tuple.Create("B", "2"), 
                    Tuple.Create("C", "3") 
                };
string tpljoined = string.Join(", ", 
                        input.Select(x => "['" + x.Item1 + "','" + x.Item2 + "']"));
Mohit S
  • 13,723
  • 6
  • 34
  • 69
1

You can also use LINQ:

var input = new List<Tuple<string, string>> { Tuple.Create("A", "1"), Tuple.Create("B", "2") };
var result = String.Join(",", input.Select(elem => "[ '" + elem.Item1 + "', '" + elem.Item2 + "']"));
Console.WriteLine(result);
w.b
  • 11,026
  • 5
  • 30
  • 49