在 C# 中创建逗号分隔的字符串 [英] Create a comma-separated strings in C#
本文介绍了在 C# 中创建逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含许多值的对象,其中一些(不是对象中的所有值)需要放入 CSV 字符串.我的方法是这样的:
I have an object which holds many values, and 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 ....
有没有更好、更优雅的方式?
Is 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 string
作为第二个参数,如下所示:
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屋!
查看全文