convert generic string list to one string

  • Thread starter Thread starter gs
  • Start date Start date
G

gs

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
 
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<string> 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();
}
 
Marc Gravell said:
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<string> 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());
 
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
 
Marc Gravell said:
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

:)
 
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.
 
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
 
thank you for the concern. That has been take care of. I am using
stringboard and its append method.

Marc Gravell said:
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
 
oops, pressed the enter key instead of backspace while correcting the
StingBuilder typo. my apology..

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

Marc Gravell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top