473,320 Members | 2,035 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,320 software developers and data experts.

Generic List to String

Hello,

How can I transform a Generic List(Of String) to a string as follows:

"value1,value2,value3,value4, ..."

Thanks,
Miguel

Mar 28 '07 #1
4 13120
On Mar 28, 8:13 pm, "shapper" <mdmo...@gmail.comwrote:
Hello,

How can I transform a Generic List(Of String) to a string as follows:

"value1,value2,value3,value4, ..."
Either:
string tmp = "";
foreach( string x in myGenericList )
{
tmp += x + ",";
}

OR....!
Do a search for LVK
(Lasse Vågsæther Karlsen open source project that solves all the
problems you ever thought you had regarding manipulating generic
collections... ;)
Thomas

--
http://ajaxwidgets.com
ASP.NET 2.0 Ajax Widgets

Mar 28 '07 #2
Hi Shapper and Thomas,

I'd would not recommend this approach as is inefficient (string
concatenation). This is much faster:

Public Shared Function ConcatList(Of T)( _
ByVal list As System.Collections.Generic.IList(Of T), _
ByVal separator As Char) As String

Dim text As System.Text.StringBuilder = _
New System.Text.StringBuilder()

For i as Integer = 0 To list.Count - 1
text.Append(list(i))
text.Append(separator)
Next

If text.Length 0 Then
text.Length = text.Length - 1
End If

Return text.ToString()
End Function
Hope this helps
--
Milosz
"Thomas Hansen" wrote:
On Mar 28, 8:13 pm, "shapper" <mdmo...@gmail.comwrote:
Hello,

How can I transform a Generic List(Of String) to a string as follows:

"value1,value2,value3,value4, ..."

Either:
string tmp = "";
foreach( string x in myGenericList )
{
tmp += x + ",";
}

OR....!
Do a search for LVK
(Lasse Vågsæther Karlsen open source project that solves all the
problems you ever thought you had regarding manipulating generic
collections... ;)
Thomas

--
http://ajaxwidgets.com
ASP.NET 2.0 Ajax Widgets

Mar 28 '07 #3
Try:

//C#
StringBuilder builder = new StringBuilder();

for (int i=0;i<list.Count;i++)
{
if(i!=0)
builder.Append(",");

builder.Append(list[i]);
}

return builder.ToString();

'VB.NET
Dim builder As New StringBuilder()

For (i as Integer = 0 to list.Count - 1)

If i <0 Then
builder.Append(",")
End If

builder.Append(list(i))

Next

return builder.ToString()

It avoids having to clip off the last item as Milosz has done.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!
*********************************************
"shapper" <md*****@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Hello,

How can I transform a Generic List(Of String) to a string as follows:

"value1,value2,value3,value4, ..."

Thanks,
Miguel
Mar 28 '07 #4
Hi Gregory,

I did cliping to avoid if-ing in the loop (which I suspect for large counts
is slower than clipping).

Regards
--
Milosz
"Cowboy (Gregory A. Beamer)" wrote:
Try:

//C#
StringBuilder builder = new StringBuilder();

for (int i=0;i<list.Count;i++)
{
if(i!=0)
builder.Append(",");

builder.Append(list[i]);
}

return builder.ToString();

'VB.NET
Dim builder As New StringBuilder()

For (i as Integer = 0 to list.Count - 1)

If i <0 Then
builder.Append(",")
End If

builder.Append(list(i))

Next

return builder.ToString()

It avoids having to clip off the last item as Milosz has done.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!
*********************************************
"shapper" <md*****@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Hello,

How can I transform a Generic List(Of String) to a string as follows:

"value1,value2,value3,value4, ..."

Thanks,
Miguel
Mar 29 '07 #5

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...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
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 { ... }
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
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...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
3
by: Peter Olcott | last post by:
How does not specify the sort criteria for Generic.List ?? The way that this is done in C++ STL is to implement operator<(), how is this done in C# and DotNet for Generic.List ???
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...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.