How do I create a comma delimited string from an ArrayList?

前端 未结 6 2283
野的像风
野的像风 2020-12-12 23:15

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 w

6条回答
  •  旧巷少年郎
    2020-12-12 23:53

    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:

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

    ...in 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.

    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!

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

提交回复
热议问题