Creating a delimited string

  • Thread starter Thread starter Raterus
  • Start date Start date
R

Raterus

Very often while programming, I need to create a delimited string with various values.

value1;value2;value3

Here is the problem I run into, and this isn't that big of deal, I always work around it.
I either have to check to see if it is the first value I added, and not put the delimiter, which each subsequent value would be delimiter & value,
or
after I add all the values, I remove the delimiter from the end.

Either works fine, I was just wondering if there is a more efficient, easier to code, way I could be going about this.

I was looking at the string builder class, and I didn't see this ability, but if I could load all the values, then perform a tostring that would delimit them for me, that would work so much easier! Maybe there are some other objects out there that could do this, or even I could extend the StringBuilderClass

Anyone have any ideas?
--Michael
 
Very often while programming, I need to create a delimited string with various values.

value1;value2;value3

Here is the problem I run into, and this isn't that big of deal, I always work around it.
I either have to check to see if it is the first value I added, and not put the delimiter, which each subsequent value would be delimiter & value,
or
after I add all the values, I remove the delimiter from the end.

Either works fine, I was just wondering if there is a more efficient, easier to code, way I could be going about this.

I was looking at the string builder class, and I didn't see this ability, but if I could load all the values, then perform a tostring that would delimit them for me, that would work so much easier! Maybe there are some other objects out there that could do this, or even I could extend the StringBuilderClass

Anyone have any ideas?

Look into the "Join" function.
 
You can store all the values in an array or arraylist, then use the
join function. One thing I do, is use the StringBuilder, and add the
delimeter to each one, then right before I write out the string, I
just strip the last delimeter off. This solution is great for large
amounts of data, but, for small amounts, the array solution may work
just fine. One other thing to look into is writing out a file with
ADO, defining your delimters.
Basically, there's more than one option, depending on what you need
and how much data you need to store in memory.

HTH,
Tibby
 
Back
Top