473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

convert generic string list to one string

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


Dec 12 '07 #1
9 16200
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


Dec 12 '07 #2
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<stringvalues,
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();
}
Dec 12 '07 #3
Marc Gravell <ma**********@gmail.comwrote:
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<stringvalues,
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 12 '07 #4
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
Dec 13 '07 #5
Marc Gravell <ma**********@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 13 '07 #6
gs
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" <gs@dontMail.teluswrote in message
news:ed**************@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


Dec 13 '07 #7
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

Dec 14 '07 #8
GS
thank you for the concern. That has been take care of. I am using
stringboard and its append method.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:f2**********************************@d27g2000 prf.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

Dec 14 '07 #9
GS
oops, pressed the enter key instead of backspace while correcting the
StingBuilder typo. my apology..

"GS" <gs**********************@msnews.Nomail.comwrote in message
news:eB**************@TK2MSFTNGP03.phx.gbl...
thank you for the concern. That has been take care of. I am using
stringboard and its append method.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:f2**********************************@d27g2000 prf.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


Dec 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
14
by: Web learner | last post by:
In the following code, I want ArrayList object x to be of type double. How can I do that? --Thanks SqlConnection objConnection = new SqlConnection("server=(local)\\SQLEXPRESS;...
0
by: Wiktor Zychla [C# MVP] | last post by:
We do have generic classes, methods and delegates. My question is: what reason prevents us from having generic properties and indexers? // impossible public List<T> GetList<T> { get { ... }
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
2
by: CodeMonkey775 | last post by:
I'm having problems passing a variable to a method which is executed and compiled using CodeDom. The situation is I have a List<CellData> with cells, each containing a formula (like Excel). I am...
2
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists,...
2
by: Chris Kennedy | last post by:
I am getting a generic list of objects from a webservice. The list is converted to an array by this process. How do I convert the array back to a list and is there any way of finding an object...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.