Convert String Array Into String – C# LINQ

A couple months ago, I had written some posts on converting String and Char Arrays

Convert a String Array to a Decimal Array using C# or VB.NET

Convert Char Array to String and Vice Versa

Here’s a very simple way if you want to convert a String Array to String using C# LINQ

static void Main(string[] args)
{
string[] indiaCityVisit = {
"Delhi", "Jodhpur", "Mumbai", "Pune", "Agra",
"Shimla", "Bengaluru", "Mysore", "Ooty",
"Jaipur", "Nagpur", "Amritsar", "Hyderabad",
"Goa", "Ahmedabad" };

string cities = String.Join(",", indiaCityVisit
.Select(s => s.ToString())
.ToArray());
Console.WriteLine(cities);

Console.ReadLine();
}
LINQ String Array to String





About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

2 comments:

David V said...

The LINQ query doesn't do anything here. All the work is done by String.Join.

Anonymous said...

string cities = String.Join(",", indiaCityVisit);