What is the easiest way to trim the values ​​of a string []?

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

And you want to cross out its meanings. Of course, you can use foreach-loop, which is probably the easiest.

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

This will lead to the following: First, Second, Third, Fourth, Fifth,

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

+4
source share
2 answers

You can use String.Join:

string result = String.Join(", ", a);
+9
source

You don’t need a loop at all. Simple string.Joinwill do.

Console.WriteLine(string.Join(", ", a));
+5
source

Source: https://habr.com/ru/post/1540371/


All Articles