Reputation: 191
In C# I have a list like this:
List<string> cities = new List<string>();
cities.Add("New York");
cities.Add("Mumbai");
cities.Add("Berlin");
cities.Add("Istanbul");
I want to save the items of the list in a string variable like this:
string info = "'New York', 'Mumbai', 'Berlin', 'Istanbul'";
How can I achieve this?
Upvotes: 0
Views: 78
Reputation: 45947
Use Join()
and Linq Select()
string info = string.Join(", ", cities.Select(x => '\'' + x + '\''));
or accoring to Tim's suggestion (C# 6.0)
string info = string.Join(", ", cities.Select(c => $"'{c}'"));
Upvotes: 3