C# 将数组转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13426463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:31:09  来源:igfitidea点击:

Convert an array to string

c#arraysstring

提问by Rob

How do I make this output to a string?

如何将此输出转换为字符串?

List<string> Client = new List<string>();
foreach (string listitem in lbClients.SelectedItems)
{
    Client.Add(listitem);
}

采纳答案by CodeLikeBeaker

You can join your array using the following:

您可以使用以下方法加入您的阵列:

string.Join(",", Client);

Then you can output anyway you want. You can change the comma to what ever you want, a space, a pipe, or whatever.

然后你可以随意输出。您可以将逗号更改为您想要的任何内容,空格、管道或其他任何内容。

回答by adv12

You probably want something like this overload of String.Join:

你可能想要像 String.Join 这样的重载:

String.Join<T> Method (String, IEnumerable<T>)

String.Join<T> Method (String, IEnumerable<T>)

Docs:

文档:

http://msdn.microsoft.com/en-us/library/dd992421.aspx

http://msdn.microsoft.com/en-us/library/dd992421.aspx

In your example, you'd use

在你的例子中,你会使用

String.Join("", Client);

String.Join("", Client);

回答by Cleber Pessoal

My suggestion:

我的建议:

using System.Linq;

string myStringOutput = String.Join(",", myArray.Select(p => p.ToString()).ToArray());

reference: https://coderwall.com/p/oea7uq/convert-simple-int-array-to-string-c

参考:https: //coderwall.com/p/oea7uq/convert-simple-int-array-to-string-c