C# Array.ToString() 是否提供有用的输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2245581/
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
Does Array.ToString() provide a useful output?
提问by PositiveGuy
If I have an array and perform a ToString()
does that just string together the array values in one long comma seperated string or is that not possible on an array?
如果我有一个数组并执行 aToString()
是否只是将数组值串在一个长逗号分隔字符串中,或者这在数组上是不可能的?
采纳答案by Adam Robinson
Option 1
选项1
If you have an array of string
s, then you can use String.Join
:
如果您有一个string
s数组,则可以使用String.Join
:
string[] values = ...;
string concatenated = string.Join(",", values);
Option 2
选项 2
If you're dealing with an array of any other type and you're using .NET 3.5 or above, you can use LINQ:
如果您正在处理任何其他类型的数组并且您使用的是 .NET 3.5 或更高版本,则可以使用 LINQ:
string concatenated = string.Join(",",
values.Select(x => x.ToString()).ToArray());
回答by Jon Skeet
You can certainly do that, but it's not the default behaviour. The easiest way to do that (from .NET 3.5 anyway) is probably:
您当然可以这样做,但这不是默认行为。最简单的方法(从 .NET 3.5 开始)可能是:
string joined = string.Join(",", array.Select(x => x.ToString()).ToArray());
MoreLINQhas a built-in method to do this:
MoreLINQ有一个内置的方法来做到这一点:
string joined = array.ToDelimitedString();
or specify the delimited explicitly:
或明确指定分隔符:
string joined = array.ToDelimitedString(",");
回答by bang
No, ToString of an array will give you the Type name string of the object. use String.Join method instead.
不,数组的 ToString 将为您提供对象的类型名称字符串。改用 String.Join 方法。
回答by Grzenio
It doesn't (as you noticed).
它没有(如您所见)。
For string arrays you can use:
对于字符串数组,您可以使用:
string.Join(",", myArray)
for other arrays I think you need to code it yourself.
对于其他数组,我认为您需要自己编写代码。
回答by Sam Salisbury
To achieve this effect you should call String.Join(string, string[])
为了达到这个效果,你应该调用 String.Join(string, string[])
I.e.
IE
string[] stringArray = new string[] { "a", "b", "c" };
string.Join(",", stringArray);
回答by v01pe
You can use string.Concat(Object[] args)
. This calls the ToString()
method of every object in args
. In a custom class you can override the ToString()
method to achieve custom string conversion like this:
您可以使用string.Concat(Object[] args)
. 这将调用ToString()
中每个对象的方法args
。在自定义类中,您可以覆盖该ToString()
方法以实现自定义字符串转换,如下所示:
public class YourClass
{
private int number;
public YourClass(int num)
{
number = num;
}
public override string ToString()
{
return "#" + number;
}
}
Now you can concatenate an array of instances of your custom class:
现在您可以连接自定义类的实例数组:
YourClass[] yourArray = { new YourClass(1), new YourClass(2), new YourClass(3) };
string concatenated = string.Concat(yourArray);
Unfortunatelythis method does not add any delimiters, but I found it to be elegant. The variable concatenated
will contain "#1#2#2"
.
不幸的是,这种方法没有添加任何分隔符,但我发现它很优雅。该变量concatenated
将包含"#1#2#2"
.