|
| Author |
Message |
gs
Joined: 08 Aug 2007 Posts: 2
|
Posted: Wed Dec 12, 2007 5:57 pm Post subject: convert generic string list to one string |
|
|
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 |
|
 |
Peter Bromberg [C# MVP]
Joined: 31 Oct 2007 Posts: 22
|
Posted: Wed Dec 12, 2007 5:46 pm Post subject: RE: convert generic string list to one string |
|
|
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 |
|
 |
Marc Gravell
Joined: 08 Aug 2007 Posts: 20
|
Posted: Wed Dec 12, 2007 5:55 pm Post subject: Re: convert generic string list to one string |
|
|
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 |
|
 |
Jon Skeet [C# MVP]
Joined: 08 Aug 2007 Posts: 266
|
Posted: Thu Dec 13, 2007 3:25 am Post subject: Re: convert generic string list to one string |
|
|
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 |
|
 |
Marc Gravell
Joined: 08 Aug 2007 Posts: 20
|
Posted: Thu Dec 13, 2007 2:16 am Post subject: Re: convert generic string list to one string |
|
|
> 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 |
|
 |
Jon Skeet [C# MVP]
Joined: 08 Aug 2007 Posts: 266
|
Posted: Thu Dec 13, 2007 12:49 pm Post subject: Re: convert generic string list to one string |
|
|
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
--
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 |
|
 |
gs
Joined: 08 Aug 2007 Posts: 2
|
Posted: Thu Dec 13, 2007 3:29 pm Post subject: Re: convert generic string list to one string |
|
|
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 |
|
 |
Marc Gravell
Joined: 08 Aug 2007 Posts: 20
|
Posted: Fri Dec 14, 2007 2:25 am Post subject: Re: convert generic string list to one string |
|
|
> 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 |
|
 |
GS
Joined: 08 Aug 2007 Posts: 9
|
Posted: Fri Dec 14, 2007 6:21 pm Post subject: Re: convert generic string list to one string |
|
|
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 |
|
 |
GS
Joined: 08 Aug 2007 Posts: 9
|
Posted: Fri Dec 14, 2007 6:27 pm Post subject: Re: convert generic string list to one string |
|
|
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 |
|
 |
|