473,473 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 13180
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.