473,230 Members | 1,817 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,230 software developers and data experts.

CSV to List

Hello,

How can I create a List of String from a CSV String?

For example, from:

"New York, London ,Lisbon ,Car,C# "

I would get a list with the following items:
"New York", "London", "Lisbon", "Car", "C#"

So I would take all words but ignore the spaces before and after a
comma but not the ones between words.

Should I make this with Regex?

Thanks,
Miguel
Jun 27 '08 #1
11 6771
On Thu, 26 Jun 2008 17:29:48 -0700, shapper <md*****@gmail.comwrote:
How can I create a List of String from a CSV String?

[...]
Should I make this with Regex?
I suppose you could. But IMHO, the Split() and Trim() methods of String
provide everything you need:

public List<stringCSVToList(string strInput)
{
string[] rgstrRecord = strInput.Split(new char[] { ',' });
List<stringlstrRet = new List<string>(rgstrRecord.Length);

foreach (string strColumn in rgstrRecord)
{
lstrRet.Add(strColumn.Trim());
}

return lstrRet;
}

No doubt there's a LINQ way to do all that that looks much nicer, but I've
been slacking and still can't write it off the top of my head. I'll learn
LINQ one day. :)

Pete
Jun 27 '08 #2
shapper wrote:
How can I create a List of String from a CSV String?

For example, from:

"New York, London ,Lisbon ,Car,C# "

I would get a list with the following items:
"New York", "London", "Lisbon", "Car", "C#"

So I would take all words but ignore the spaces before and after a
comma but not the ones between words.
Fundamentally:
Split
Trim
assign

C# 2.0:

public static List<stringSpecialSplit20(string s)
{
List<stringres = new List<string>();
foreach(string part in s.Split(','))
{
res.Add(part.Trim());
}
return res;
}

C# 3.5:

public static List<stringSpecialSplit30(string s)
{
return new List<string>(from part in s.Split(',') select
part.Trim());
}

Arne
Jun 27 '08 #3
On Jun 26, 5:29*pm, shapper <mdmo...@gmail.comwrote:
Hello,

How can I create a List of String from a CSV String?

For example, from:

"New York, London * ,Lisbon * *,Car,C# * *"

I would get a list with the following items:
"New York", "London", "Lisbon", "Car", "C#"

So I would take all words but ignore the spaces before and after a
comma but not the ones between words.

Should I make this with Regex?

Thanks,
Miguel

Miguel,
There are a number of CSV readers freely available. Here's my
favorite:
http://www.codeproject.com/KB/databa...ricParser.aspx

I believe this one can read directly into a List<stringand includes:
http://www.codeproject.com/KB/database/filehelpers.aspx

-Jay

Jun 27 '08 #4
On Jun 27, 1:44*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
No doubt there's a LINQ way to do all that that looks much nicer, but I've *
been slacking and still can't write it off the top of my head. *I'll learn *
LINQ one day. *:)
strInput.Split(',').Select(x =x.Trim()).ToList()

:)

Jon
Jun 27 '08 #5
On Jun 27, 1:52*am, Arne Vajhřj <a...@vajhoej.dkwrote:

<snip>

Just wearing my version number pedantry hat:
C# 3.5:
It's C# 3, not 3.5. I've got an article about the version numbers at
http://csharpindepth.com/Articles/Ch.../Versions.aspx

(I know I've been doing a lot of this recently. I can understand if
it's slightly annoying, but I think it's for the best in the long run.
If other groupies would rather I stopped, please let me know.)
* * * * *public static List<stringSpecialSplit30(string s)
* * * * *{
* * * * * * *return new List<string>(from part in s.Split(',') select
part.Trim());
* * * * *}
I've just posted my version as a reply to Pete's post - I tend not to
bother with a query expression if I'm just using one operator (just
select, or a where followed by a no-op select). And ToList is
goodness :)

Jon
Jun 27 '08 #6
On Thu, 26 Jun 2008 22:54:58 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
Just wearing my version number pedantry hat:
>C# 3.5:

It's C# 3, not 3.5. I've got an article about the version numbers at
http://csharpindepth.com/Articles/Ch.../Versions.aspx

(I know I've been doing a lot of this recently. I can understand if
it's slightly annoying, but I think it's for the best in the long run.
If other groupies would rather I stopped, please let me know.)
For what it's worth, I find the version corrections useful, even if it's
unlikely I'm ever going to get everything memorized. If anything, as more
incremental versions come out, and as there become more combinations of
CLR, C#, and .NET versions available, I think it's more and more important
to be precise about which features require which versions.

Heck, just the other day I had to unconfuse myself about which .NET
versions include the various Action delegates, since it turns out two
showed up in 3.0 and the other three showed up in 3.5! I find it helpful
to have other folks piping up to keep me honest.

That said, I realize opinions may vary. That's just my two cents. :)

Pete
Jun 27 '08 #7
about which .NET
versions include the various Action delegates, since it turns out two
showed up in 3.0 and the other three showed up in 3.5!
2.0, surely? I could well be wrong, but Action<Tis used in
List<T>.ForEach since 2.0?

Marc
Jun 27 '08 #8
Just wearing my version number pedantry hat:

Now if only we could fix the number of books touting C# 2008 ;-p

Marc
Jun 27 '08 #9
On Fri, 27 Jun 2008 00:27:05 -0700, Marc Gravell <ma**********@gmail.com>
wrote:
>about which .NET
versions include the various Action delegates, since it turns out two
showed up in 3.0 and the other three showed up in 3.5!

2.0, surely? I could well be wrong, but Action<Tis used in
List<T>.ForEach since 2.0?
Yes, sorry. Typo.

However, I was mistaken. Only Action<Twas in 2.0. The rest all appear
to not have shown up until 3.5 (I mistakenly assumed that the
parameterless Action type had been around as long as Action<T>).

Pete
Jun 27 '08 #10
On Jun 27, 8:57*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 27 Jun 2008 00:27:05 -0700, Marc Gravell <marc.grav...@gmail.com>*
wrote:
about which .NET
versions include the various Action delegates, since it turns out two
showed up in 3.0 and the other three showed up in 3.5!
2.0, surely? I could well be wrong, but Action<Tis used in
List<T>.ForEach since 2.0?

Yes, sorry. *Typo.

However, I was mistaken. *Only Action<Twas in 2.0. *The rest all appear *
to not have shown up until 3.5 (I mistakenly assumed that the *
parameterless Action type had been around as long as Action<T>).

Pete
Thank you all!

For now I think I will use the Linq version.
It is short and it solves my particular problem.

But the other suggestions might be handy in the future.

Thank You,
Miguel
Jun 27 '08 #11
On Fri, 27 Jun 2008 04:24:20 -0700, shapper <md*****@gmail.comwrote:
For now I think I will use the Linq version.
It is short and it solves my particular problem.

But the other suggestions might be handy in the future.
Except for possibly Jay's, probably not. Jon's LINQ suggestion is
essentially identical to the code examples posted to this thread. It's
just a lot more concise. :)

Pete
Jun 27 '08 #12

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

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnšt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.