문제

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?

도움이 되었습니까?

해결책

You can use String.Join:

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

다른 팁

You don't need looping at all. A simple string.Join would do.

Console.WriteLine(string.Join(", ", a));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top