89

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?

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.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140

8 Answers8

167

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())
Dillie-O
  • 29,277
  • 14
  • 101
  • 140
  • 2
    There are other "gotchas". One being that this solution isn't as idiomatic as looping through the list yourself. Second, if ToArray traverses the collection, and Join does too, this takes twice as long as a simple foreach loop. – Bill the Lizard Oct 17 '08 at 18:38
  • Of course there's always the possibility of using System.Collections.Specialized.StringCollection. – ICR Oct 17 '08 at 18:38
  • If you can consider using a List instead, that has a built-in ToArray() method, and eliminiates type issues. BTW - typeof(string) is preferable to Type.GetType("System.String"). – Jon B Oct 17 '08 at 18:39
17

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Aren't CType and CStr are essentially calls into the Convert class? – Joel Coehoorn Oct 17 '08 at 19:16
  • @Echostorm: where do I say that? @Joel: not at all. Most of the time, they call the `MS.VB.CS.Conversions.ToString` helper function which results in a simple `ToString` call. But even this method I wouldn't call *direct*y (as opposed to through the VB cast operators). – Konrad Rudolph Oct 18 '08 at 13:39
16

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));
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
mspmsp
  • 953
  • 6
  • 7
  • oops.... that's not right... how about: string.Join(",", Array.ConvertAll(a.ToArray(), Convert.ToString)); – mspmsp Oct 17 '08 at 18:47
8

string.Join(" ,", myArrayList.ToArray()); This will work with .net framework 4.5

bashburak
  • 105
  • 1
  • 5
2

Here's a simple example demonstrating the creation of a comma delimited string using String.Join() from a list of Strings:

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());
Jim Lahman
  • 2,691
  • 2
  • 25
  • 21
2
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.

Echostorm
  • 9,678
  • 8
  • 36
  • 50
  • Wow, I'm really sorry! I completely overlooked the “C#” tag on the question and surmised your answers were meant to be valid VB code. Shame on me. But while we're nitpicking: your second answer appends an extra “,” at the end. – Konrad Rudolph Oct 18 '08 at 13:03
0

So far I found this is a good and quick solution

//CPID[] is the array
string cps = "";
if (CPID.Length > 0)
{   
    foreach (var item in CPID)
    {
        cps += item.Trim() + ",";
    }
}
//Use the string cps
Monzur
  • 1,341
  • 14
  • 11
-1

Just because the answer I liked, had the comma the wrong way around making a CSV list of names look like this:

Rob ,Steve ,Ben

Instead this is what you should want:

Rob, Steve, Ben

So only a small change will be needed using the latest VS/C# 2022:

ArrayList names = new ArrayList();

names.Add("Rob");
names.Add("Steve");
names.Add("Ben");

string namelist = String.Join(", ", names.ToArray());

Console.WriteLine(namelist);
Jonathan Barraone
  • 489
  • 1
  • 3
  • 20
Monkey Magix
  • 163
  • 2
  • 3
  • 10