C# 如何从 ArrayList 创建逗号分隔的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/213295/
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
How do I create a comma delimited string from an ArrayList?
提问by Dillie-O
I'm storing an ArrayList of Ids in a processing script that I want to spit out as a comma delimited list for output to the debug log. Is there a way I can get this easily without looping through things?
我将 Id 的 ArrayList 存储在处理脚本中,我想将其作为逗号分隔的列表输出到调试日志中。有没有一种方法可以轻松地做到这一点而无需遍历事物?
EDIT: Thanks to Joel for pointing out the List(Of T) that is available in .net 2.0 and above. That makes things TONS easier if you have it available.
编辑:感谢 Joel 指出在 .net 2.0 及更高版本中可用的 List(Of T)。如果你有它,这会让事情变得更容易。
采纳答案by Dillie-O
Yes, I'm answering my own question, but I haven't found it here yet and thought this was a rather slick thing:
是的,我正在回答我自己的问题,但我还没有在这里找到它,并认为这是一件相当巧妙的事情:
...in VB.NET:
...在 VB.NET 中:
String.Join(",", CType(TargetArrayList.ToArray(Type.GetType("System.String")), String()))
...in C#
...在 C# 中
string.Join(",", (string[])TargetArrayList.ToArray(Type.GetType("System.String")))
The only "gotcha" to these is that the ArrayList must have the items stored as Strings if you're using Option Strict to make sure the conversion takes place properly.
唯一的“问题”是如果您使用 Option Strict 以确保正确进行转换,则 ArrayList 必须将项目存储为字符串。
EDIT: If you're using .net 2.0 or above, simply create a List(Of String) type object and you can get what you need with. Many thanks to Joel for bringing this up!
编辑:如果您使用 .net 2.0 或更高版本,只需创建一个 List(Of String) 类型的对象,您就可以获得所需的内容。非常感谢乔尔提出这个问题!
String.Join(",", TargetList.ToArray())
回答by mspmsp
Something like:
就像是:
String.Join(",", myArrayList.toArray(string.GetType()) );
Which basically loops ya know...
这基本上是循环你知道......
EDIT
编辑
how about:
怎么样:
string.Join(",", Array.ConvertAll<object, string>(a.ToArray(), Convert.ToString));
回答by Echostorm
foo.ToArray().Aggregate((a, b) => (a + "," + b)).ToString()
or
或者
string.Concat(foo.ToArray().Select(a => a += ",").ToArray())
Updating, as this is extremely old. You should, of course, use string.Join now. It didn't exist as an option at the time of writing.
更新,因为这是非常旧的。当然,您现在应该使用 string.Join。在撰写本文时,它不作为选项存在。
回答by Konrad Rudolph
The solutions so far are all quite complicated. The idiomatic solution should doubtless be:
到目前为止的解决方案都非常复杂。惯用的解决方案无疑应该是:
String.Join(",", x.Cast(Of String)().ToArray())
There's no need for fancy acrobatics in new framework versions. Supposing a not-so-modern version, the following would be easiest:
在新的框架版本中不需要花哨的技巧。假设一个不那么现代的版本,以下是最简单的:
Console.WriteLine(String.Join(",", CType(x.ToArray(GetType(String)), String())))
mspmsp's second solution is a nice approach as well but it's not working because it misses the AddressOf
keyword. Also, Convert.ToString
is rather inefficient (lots of unnecessary internal evaluations) and the Convert
class is generally not very cleanly designed. I tend to avoid it, especially since it's completely redundant.
mspmsp 的第二个解决方案也是一个不错的方法,但它不起作用,因为它错过了AddressOf
关键字。此外,Convert.ToString
效率相当低(很多不必要的内部评估),并且该Convert
课程通常设计得不是很干净。我倾向于避免它,特别是因为它完全是多余的。
回答by Jim Lahman
Here's a simple example demonstrating the creation of a comma delimited string using String.Join() from a list of Strings:
这是一个简单的示例,演示了使用 String.Join() 从字符串列表创建逗号分隔的字符串:
List<string> histList = new List<string>();
histList.Add(dt.ToString("MM/dd/yyyy::HH:mm:ss.ffff"));
histList.Add(Index.ToString());
/*arValue is array of Singles */
foreach (Single s in arValue)
{
histList.Add(s.ToString());
}
String HistLine = String.Join(",", histList.ToArray());
回答by bashburak
string.Join(" ,", myArrayList.ToArray());
This will work with .net framework 4.5
string.Join(" ,", myArrayList.ToArray());
这将适用于 .net 框架 4.5