如何创建从一个ArrayList分隔字符串逗号? [英] How do I create a comma delimited string from an ArrayList?

查看:1181
本文介绍了如何创建从一个ArrayList分隔字符串逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储id的ArrayList中,我要吐出一个逗号分隔的列表输出到调试日志处理脚本。 ?有没有一种方法我能得到这个不容易的事情,通过循环

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?

编辑:感谢为乔尔指出列表(Of T)已可用在.NET 2.0以上。这使事情变得更容易吨如果您有它可用。

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.

推荐答案

是的,我回答我的问题,但我的天堂 t上找不到它在这里没有,认为这是一个相当漂亮的事情:

Yes, I'm answering my own question, but I haven't found it here yet and thought this was a rather slick thing:

...在VB.NET:

...in VB.NET:

String.Join(",", CType(TargetArrayList.ToArray(Type.GetType("System.String")), String()))

...在C#

string.Join(",", (string[])TargetArrayList.ToArray(Type.GetType("System.String")))

唯一的疑难杂症这些是ArrayList中必须有,如果您使用选项严格,以确保发生转换正确为字符串存储的项目。

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.

编辑:如果您使用.NET 2.0或以上,只需创建一个列表(字符串)类型的对象,你可以得到你需要什么。非常感谢乔尔提出这个了!

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!

String.Join(",", TargetList.ToArray())

这篇关于如何创建从一个ArrayList分隔字符串逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆