Trying to stay completely language agnostic, and avoiding built in methods like Split() and Join(), what are the most utilized or accepted methods to build a CSV string? I run into situations like this a lot, and I’m curious as to how methods like Split() implement this? I usually do something like this:
for(int i = 0; i < list.length; i++)
{
if(i == list.length - 1)
{
Write(list[i]);
}
else
{
Write(list[i] + ',');
}
}
But it seems like there should be a better way to do it.
Most implementations I’ve seen do something more like:
This avoids the checking inside the for loop. The .NET Framework’s Join() method uses this basic approach (with a lot more checking, of course).