473,382 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

convert (list)

Hello,
I am looking for a way to convert list<stringto string (one string
conainig all list members).
How can I do it in an elegant way ? (I mean without foreach and
appendind strings)
Thank u!

*** Sent via Developersdex http://www.developersdex.com ***

Jun 27 '08 #1
4 2912
csharpula csharp skrev:
Hello,
I am looking for a way to convert list<stringto string (one string
conainig all list members).
How can I do it in an elegant way ? (I mean without foreach and
appendind strings)
Thank u!
I don't think that it is an easy way.

However: Use a StringBuilder object in your foreach loop to speed up
concatenation, and end with a StringBuilder.ToString() to get the final
string.

--
Bjorn Brox
Jun 27 '08 #2
csharpula csharp wrote:
Hello,
I am looking for a way to convert list<stringto string (one string
conainig all list members).
How can I do it in an elegant way ? (I mean without foreach and
appendind strings)
Thank u!
String s = String.Join(Environment.NewLine, list.ToArray());

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #3
Note that if you have an array (not a list), then string.Join() is
useful - but it isn't realy worth doing .ToArray() just to use
string.Join().

With a List<string>, StringBuilder is the way to go; this is perhaps
something that befits an "extension" method in C# 3 - the following
works in VS2008, for example:

// usage
static void Main()
{
var items = new List<string{"abc", "def", "ghi"};
string csv = items.Concat(",");
}

// (in some utility library somewhere)
public static string Concat(this IEnumerable<stringitems,
string separator)
{
if (items == null) throw new
ArgumentNullException("items");
StringBuilder sb = new StringBuilder();
foreach (string item in items)
{
if (sb.Length 0) sb.Append(separator);
sb.Append(item);
}
return sb.ToString();
}
Jun 27 '08 #4
self(example; cited Concat)

Should have called it Join... oops
Jun 27 '08 #5

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

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
5
by: Andrew V. Romero | last post by:
At work we have an excel file that contains the list of medications and their corresponding strengths. I would like to save the excel file as a text list and paste this list into a javascript...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
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...
1
by: Spam Catcher | last post by:
For example, I have the following: Dim MyObjectArray() as Object = External.Function() Is it possible to convert MyObjectArray into a Generics Collection.List? Thanks.
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
1
by: pnbaocuong | last post by:
Dear All When I try to use convert function of MS sql server like this: String sql = " (employee_id = '" + employee_id + "') and ((convert(VARCHAR,w_date,103) = '" + w_date + "'))"; ...
2
by: Bart Kastermans | last post by:
I have written a little program that takes as input a text file, converts it to a list with appropriate html coding (making it into a nice table). Finally I want to upload this list as a textfile...
10
by: =?Utf-8?B?Um95?= | last post by:
What is the way to have best performance to copy a byte to a value such as long? I use BitConverter.ToInt64(binary, offset) But the performace is not good enough. I need to have the best...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.