string.Join on a List<int>或其他类型 [英] string.Join on a List&lt;int&gt; or other type

查看:37
本文介绍了string.Join on a List<int>或其他类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个数组或整数列表转换为逗号分隔的字符串,如下所示:

I want to turn an array or list of ints into a comma delimited string, like this:

string myFunction(List<int> a) {
    return string.Join(",", a);
}

但是 string.Join 只将 List 作为第二个参数.这样做的最佳方法是什么?

But string.Join only takes List<string> as the second parameter. What is the best way to do this?

推荐答案

最好的方法是升级到 .NET 4.0,那里有一个可以满足您要求的重载:

The best way is to upgrade to .NET 4.0 where there is an overload that does what you want:

如果不能升级,可以使用Select和ToArray达到同样的效果.

If you can't upgrade, you can achieve the same effect using Select and ToArray.

    return string.Join(",", a.Select(x => x.ToString()).ToArray());

这篇关于string.Join on a List<int>或其他类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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