By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Creating a delimited string

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
String.Join(";", New String() { value1, value2, value3})

ChrisG
Nov 20 '05 #2

P: n/a
In article <uO**************@TK2MSFTNGP10.phx.gbl>, ra*****@spam.org
says...

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.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #3

P: n/a
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
On Mon, 12 Jul 2004 10:26:12 -0400, "Raterus" <ra*****@spam.org>
wrote:
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


Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.