创建逗号分隔字符串C#? [英] Create comma separated strings C#?

查看:111
本文介绍了创建逗号分隔字符串C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有拥有许多的价值观,他们中的一些(而不是从对象的值)需要被放在一个CSV字符串的对象。我的做法是这样的:

I have an object which holds many values, some of them (not all values from the object) need to be put in a csv string. My approach was this:

string csvString = o.number + "," + o.id + "," + o.whatever ....

不知怎的,我觉得有一个更好,更优雅的方式?

Somehow I think there is a better, more elegant way?

推荐答案

如果你把所有的值在数组中,至少可以使用的string.join

If you put all your values in an array, at least you can use string.Join.

string[] myValues = new string[] { ... };
string csvString = string.Join(",", myValues);

您也可以使用的过载的string.join 这需要 PARAMS字符串为这样的第二个参数:

You can also use the overload of string.Join that takes params string as the second parameter like this:

string csvString = string.Join(",", value1, value2, value3, ...);

这篇关于创建逗号分隔字符串C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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