Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to loop out values of string[]?

So, let's say that I have a string[] { "First", "Second", "Third", "Fourth", "Fifth" }; called "a".

And you want to loop out the values of it. Of course, you could use foreach-loop, that's probably the easiest.

foreach (string i in a)
{
    Console.Write(i + ", ");
}

This would output the following: First, Second, Third, Fourth, Fifth,

Notice that the last index has a comma after it. Now, how would you loop the same way, leaving the last index without a comma and a white space?

like image 479
Tatu Avatar asked Dec 09 '22 08:12

Tatu


1 Answers

You can use String.Join:

string result = String.Join(", ", a);
like image 131
Tim Schmelter Avatar answered Dec 11 '22 06:12

Tim Schmelter