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.
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));
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.
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
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());
string.Join(" ,", myArrayList.ToArray());
This will work with .net framework 4.5
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());
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.
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
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.
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));
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.
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.
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.
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));
string.Join(" ,", myArrayList.ToArray());
This will work with .net framework 4.5
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));
Source: Stackoverflow.com