MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

convert generic string list to one string

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp
Author Message
gs



Joined: 08 Aug 2007
Posts: 2

PostPosted: Wed Dec 12, 2007 5:57 pm    Post subject: convert generic string list to one string Reply with quote

is there any built in function or dotnet framework(version 2) to merge a
generic list of string into one string with each element delimited by
specified delimiting string?

or do I have to roll my own/ IT is not hard to roil my own but hate to
re-invent the wheel. I have searched built-in help. Google but failed to
used the right search term to come anything good

Archived from group: microsoft>public>dotnet>languages>csharp
Back to top
View user's profile Send private message
Peter Bromberg [C# MVP]



Joined: 31 Oct 2007
Posts: 22

PostPosted: Wed Dec 12, 2007 5:46 pm    Post subject: RE: convert generic string list to one string Reply with quote

Try using the CopyTo method, and then taking the String.Join method against
that.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com


"gs" wrote:

> is there any built in function or dotnet framework(version 2) to merge a
> generic list of string into one string with each element delimited by
> specified delimiting string?
>
> or do I have to roll my own/ IT is not hard to roil my own but hate to
> re-invent the wheel. I have searched built-in help. Google but failed to
> used the right search term to come anything good
>
>
>
>
>
Back to top
View user's profile Send private message
Marc Gravell



Joined: 08 Aug 2007
Posts: 20

PostPosted: Wed Dec 12, 2007 5:55 pm    Post subject: Re: convert generic string list to one string Reply with quote

One line?
string value = string.Join(separator, list.ToArray());

Although from a purist view it would be more efficient to iterate on
the values themselves (rather than forcing an array for no real
reason). You can do this with a simple utility method, or in C# 3,
perhaps an extension method (although "Join" is a poor choice of name,
as it might clash with LINQ's Join):

string value = someSetOfStrings.Join(separator); // example
usage
...
public static string Join(this IEnumerable values,
string separator) {
StringBuilder sb = new StringBuilder();
bool first = true;
foreach (string value in values) {
if (first) {
first = false;
} else {
sb.Append(separator);
}
sb.Append(value);
}
return sb.ToString();
}
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 266

PostPosted: Thu Dec 13, 2007 3:25 am    Post subject: Re: convert generic string list to one string Reply with quote

Marc Gravell wrote:
> One line?
> string value = string.Join(separator, list.ToArray());
>
> Although from a purist view it would be more efficient to iterate on
> the values themselves (rather than forcing an array for no real
> reason). You can do this with a simple utility method, or in C# 3,
> perhaps an extension method (although "Join" is a poor choice of name,
> as it might clash with LINQ's Join):
>
> string value = someSetOfStrings.Join(separator); // example
> usage
> ...
> public static string Join(this IEnumerable values,
> string separator) {
> StringBuilder sb = new StringBuilder();
> bool first = true;
> foreach (string value in values) {
> if (first) {
> first = false;
> } else {
> sb.Append(separator);
> }
> sb.Append(value);
> }
> return sb.ToString();
> }

This is a great case for Aggregate:

words.Aggregate(new StringBuilder(),
(sb, word) => sb.Append(word).Append(separator),
sb => sb.ToString());

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
Marc Gravell



Joined: 08 Aug 2007
Posts: 20

PostPosted: Thu Dec 13, 2007 2:16 am    Post subject: Re: convert generic string list to one string Reply with quote

> This is a great case for Aggregate:

Well, one too many separators, but a tidy job I must admit.
Of course, it fails the "dotnet framework(version 2)" test, but then
my C# 3 and .NET 2 [sp1] offering probably isn't what the OP wanted
either ;-p

Marc
Back to top
View user's profile Send private message
Jon Skeet [C# MVP]



Joined: 08 Aug 2007
Posts: 266

PostPosted: Thu Dec 13, 2007 12:49 pm    Post subject: Re: convert generic string list to one string Reply with quote

Marc Gravell wrote:
> > This is a great case for Aggregate:
>
> Well, one too many separators, but a tidy job I must admit.

Rats. Hmm. Probably easiest to do the "add the separator at the start
but only if the builder isn't empty" trick to avoid that.

> Of course, it fails the "dotnet framework(version 2)" test, but then
> my C# 3 and .NET 2 [sp1] offering probably isn't what the OP wanted
> either ;-p

Smile

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Back to top
View user's profile Send private message
gs



Joined: 08 Aug 2007
Posts: 2

PostPosted: Thu Dec 13, 2007 3:29 pm    Post subject: Re: convert generic string list to one string Reply with quote

thank you all for the input and insights.

I already rolled one along the line mentioned by Marc, starting with empty
string and iterate through the list


hopefully may be one of the future version of c# will provide built-in
function to do what I did to increase productivity. I will not expect that
to happen for compact framework but I do hope it will for the regular .net
for windows.



"gs" wrote in message @TK2MSFTNGP03.phx.gbl...
> is there any built in function or dotnet framework(version 2) to merge a
> generic list of string into one string with each element delimited by
> specified delimiting string?
>
> or do I have to roll my own/ IT is not hard to roil my own but hate to
> re-invent the wheel. I have searched built-in help. Google but failed to
> used the right search term to come anything good
>
>
>
>
Back to top
View user's profile Send private message
Marc Gravell



Joined: 08 Aug 2007
Posts: 20

PostPosted: Fri Dec 14, 2007 2:25 am    Post subject: Re: convert generic string list to one string Reply with quote

> starting with empty string

If you do really mean "string" here, then note that this may be more
than a little inefficient, due to the immutability of strings (so your
loop would create *lots* of GEN0 objects, and consume telescoping
memory (i.e. n1 + [n1+n2] + [n1+n2+n3] + [n1+n2+n3+n4] + ... if you
see what I mean [give-or-take the separators]).
String concatenation inside a loop is an ideal candidate for
StringBuilder (which is supported under CF).

Explanation: www.pobox.com/~skeet/csharp/stringbuilder.html

Of course, if you aren't actually using "string" concatenation then it
isn't an issue.

Marc
Back to top
View user's profile Send private message
GS



Joined: 08 Aug 2007
Posts: 9

PostPosted: Fri Dec 14, 2007 6:21 pm    Post subject: Re: convert generic string list to one string Reply with quote

thank you for the concern. That has been take care of. I am using
stringboard and its append method.

"Marc Gravell" wrote in message@d27g2000prf.googlegroups.com...
> > starting with empty string
>
> If you do really mean "string" here, then note that this may be more
> than a little inefficient, due to the immutability of strings (so your
> loop would create *lots* of GEN0 objects, and consume telescoping
> memory (i.e. n1 + [n1+n2] + [n1+n2+n3] + [n1+n2+n3+n4] + ... if you
> see what I mean [give-or-take the separators]).
> String concatenation inside a loop is an ideal candidate for
> StringBuilder (which is supported under CF).
>
> Explanation: www.pobox.com/~skeet/csharp/stringbuilder.html
>
> Of course, if you aren't actually using "string" concatenation then it
> isn't an issue.
>
> Marc
>
Back to top
View user's profile Send private message
GS



Joined: 08 Aug 2007
Posts: 9

PostPosted: Fri Dec 14, 2007 6:27 pm    Post subject: Re: convert generic string list to one string Reply with quote

oops, pressed the enter key instead of backspace while correcting the
StingBuilder typo. my apology..

"GS" wrote in message@TK2MSFTNGP03.phx.gbl...
> thank you for the concern. That has been take care of. I am using
> stringboard and its append method.
>
> "Marc Gravell" wrote in message
> @d27g2000prf.googlegroups.com...
> > > starting with empty string
> >
> > If you do really mean "string" here, then note that this may be more
> > than a little inefficient, due to the immutability of strings (so your
> > loop would create *lots* of GEN0 objects, and consume telescoping
> > memory (i.e. n1 + [n1+n2] + [n1+n2+n3] + [n1+n2+n3+n4] + ... if you
> > see what I mean [give-or-take the separators]).
> > String concatenation inside a loop is an ideal candidate for
> > StringBuilder (which is supported under CF).
> >
> > Explanation: www.pobox.com/~skeet/csharp/stringbuilder.html
> >
> > Of course, if you aren't actually using "string" concatenation then it
> > isn't an issue.
> >
> > Marc
> >
>
>

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
PropertyDescriptor for Generic.List(Of String)? I have an object which I would like to show on a property grid. The object has a property that ultimately can boil down to an array of unique strings. I though I'd be clever and code it as a String). When I select the object so it is shown

Generic Connection String Hi, I need to present the user with a list of Tables from a database. After they have selected the table I need to present them with a list of Fields. After they select the Fields they require, I need to use an to retrieve the Data. It NEED

Using ForEach in a Generic List object. I have another question that has to deal with the methods that the Generic List object provides that require delegates but the delegates do not allow you to pass in parameters. I'll explain first and then try to provide an example. I have an object that

String.Replace() bug When using the method I'm encountering differing behavior on a Dell AXIM running WinCE 2003 and a Compaq iPAQ 3950 running WinCE 2002. The following code snippet is of the process in question '### begin code excerpt Const T

String.join Does string.join work only with one dimensional arrays or syntax may be different for arrays? I have sShipTo = ,", aShipTo) where aShipTo is two dimensional array.
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group