从一个IList<string>或多个IEnumerable<string>?创建逗号分隔的字符串值列表的最简洁方法是什么?
String.Join(...)上的操作string[],从而可能是麻烦的与当类型,如工作IList<string>或IEnumerable<string>不能容易地被转换成一个字符串数组.
我想将一个数组或整数列表转换为逗号分隔的字符串,如下所示:
string myFunction(List<int> a) {
return string.Join(",", a);
}
Run Code Online (Sandbox Code Playgroud)
但是string.Join只List<string>作为第二个参数.做这个的最好方式是什么?
我正在尝试将第一个示例http://www.dotnetperls.com/convert-list-string实施到我的方法中,但是我很难匹配该方法的第二个参数:
string printitout = string.Join(",", test.ToArray<Location>);
Run Code Online (Sandbox Code Playgroud)
错误信息:
The best overloaded method match for 'string.Join(string,
System.Collections.Generic.IEnumerable<string>)' has some invalid arguments
Run Code Online (Sandbox Code Playgroud)
所有的IList接口也都使用IEnurmerable实现(除非有人希望我在这里未列出)。
class IList2
{
static void Main(string[] args)
{
string sSite = "test";
string sSite1 = "test";
string sSite2 = "test";
Locations test = new Locations();
Location loc = new Location();
test.Add(sSite)
test.Add(sSite1)
test.Add(sSite2)
string printitout = string.Join(",", test.ToArray<Location>); //having issues calling what it needs.
}
}
string printitout = string.Join(",", test.ToArray<Location>);
public class Location
{
public Location()
{ …Run Code Online (Sandbox Code Playgroud)