打印每个项目后逗号(,)在一个数组 [英] Printing a comma (,) after each item in an array

查看:96
本文介绍了打印每个项目后逗号(,)在一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有项目

  A [] = [A,B,C数组(或列表) D,E] 

如果我想用逗号将它们打印出来,以便每个项目分离(或任何其他分隔符),我一般要做到这一点:

 的for(int i = 0; I< A.Count;我++)
{
Console.Write(A [I]);

如果(ⅰ= A.Count-1!)
Console.Write(,);
}



所以,我的输出是这样的:

  A,b,C,D,E 

有没有更好的或者更简洁的方式来实现这一目标?



我喜欢用foreach循环,但打印的最后一个元素后,逗号为好,这是不可取的。


解决方案

  Console.WriteLine(的string.join(,,A )); 


Lets say I have an array (or list) of items

A[] = [a,b,c,d,e]

If I want to print them out so each item is separated by a comma (or any other delimiter), I generally have to do this:

for(int i=0; i < A.Count; i++)
{
    Console.Write(A[i]);

    if (i != A.Count-1)
        Console.Write(",");
}

So, my output looks like:

a,b,c,d,e

Is there a better or neater way to achieve this?

I like to use a foreach loop, but that prints a comma after the last element as well, which is undesirable.

解决方案

Console.WriteLine(string.Join(",", A));

这篇关于打印每个项目后逗号(,)在一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆