将整数数组转换为逗号分隔的字符串

时间:2021-02-04 00:21:09

It's a simple question; I am a newbie in C#, how can I perform the following

这是一个简单的问题;我是c#的新手,如何执行以下操作

  • I want to convert an array of integers to a comma-separated string.
  • 我想把一个整数数组转换成逗号分隔的字符串。

I have

我有

int[] arr = new int[5] {1,2,3,4,5};

I want to convert it to one string

我想把它转换成一个字符串

string => "1,2,3,4,5"

5 个解决方案

#1


390  

var result = string.Join(",", arr);

This uses the following overload of string.Join:

使用下列字符串重载。join:

public static string Join<T>(string separator, IEnumerable<T> values);

#2


103  

.NET 4

net 4

string.Join(",", arr)

.NET earlier

net早些时候

string.Join(",", Array.ConvertAll(arr, x => x.ToString()))

#3


9  

int[] arr = new int[5] {1,2,3,4,5};

You can use Linq for it

您可以使用Linq

String arrTostr = arr.Select(a => a.ToString()).Aggregate((i, j) => i + "," + j);

#4


5  

You can have a pair of extension methods to make this task easier:

你可以有一对扩展方法使这个任务更容易:

public static string ToDelimitedString<T>(this IEnumerable<T> lst, string separator = ", ")
{
    return lst.ToDelimitedString(p => p, separator);
}

public static string ToDelimitedString<S, T>(this IEnumerable<S> lst, Func<S, T> selector, 
                                             string separator = ", ")
{
    return string.Join(separator, lst.Select(selector));
}

So now just:

现在是:

new int[] { 1, 2, 3, 4, 5 }.ToDelimitedString();

#5


4  

Use LINQ Aggregate method to convert array of integers to a comma separated string

使用LINQ聚合方法将整数数组转换为逗号分隔的字符串

var intArray = new []{1,2,3,4};
string concatedString = intArray.Aggregate((a, b) =>Convert.ToString(a) + "," +Convert.ToString( b));
Response.Write(concatedString);

output will be

输出将

1,2,3,4

1、2、3、4

This is one of the solution you can use if you have not .net 4 installed.

如果没有安装。net 4,这是您可以使用的解决方案之一。

#1


390  

var result = string.Join(",", arr);

This uses the following overload of string.Join:

使用下列字符串重载。join:

public static string Join<T>(string separator, IEnumerable<T> values);

#2


103  

.NET 4

net 4

string.Join(",", arr)

.NET earlier

net早些时候

string.Join(",", Array.ConvertAll(arr, x => x.ToString()))

#3


9  

int[] arr = new int[5] {1,2,3,4,5};

You can use Linq for it

您可以使用Linq

String arrTostr = arr.Select(a => a.ToString()).Aggregate((i, j) => i + "," + j);

#4


5  

You can have a pair of extension methods to make this task easier:

你可以有一对扩展方法使这个任务更容易:

public static string ToDelimitedString<T>(this IEnumerable<T> lst, string separator = ", ")
{
    return lst.ToDelimitedString(p => p, separator);
}

public static string ToDelimitedString<S, T>(this IEnumerable<S> lst, Func<S, T> selector, 
                                             string separator = ", ")
{
    return string.Join(separator, lst.Select(selector));
}

So now just:

现在是:

new int[] { 1, 2, 3, 4, 5 }.ToDelimitedString();

#5


4  

Use LINQ Aggregate method to convert array of integers to a comma separated string

使用LINQ聚合方法将整数数组转换为逗号分隔的字符串

var intArray = new []{1,2,3,4};
string concatedString = intArray.Aggregate((a, b) =>Convert.ToString(a) + "," +Convert.ToString( b));
Response.Write(concatedString);

output will be

输出将

1,2,3,4

1、2、3、4

This is one of the solution you can use if you have not .net 4 installed.

如果没有安装。net 4,这是您可以使用的解决方案之一。