CSV to List

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
shapper said:
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<string> SpecialSplit20(string s)
{
List<string> res = new List<string>();
foreach(string part in s.Split(','))
{
res.Add(part.Trim());
}
return res;
}

C# 3.5:

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

Arne
 
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/database/GenericParser.aspx

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

-Jay
 
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
 
<snip>

Just wearing my version number pedantry hat:

It's C# 3, not 3.5. I've got an article about the version numbers at
http://csharpindepth.com/Articles/Chapter1/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<string> SpecialSplit30(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
 
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<T> is used in
List<T>.ForEach since 2.0?

Marc
 
Just wearing my version number pedantry hat:

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

Marc
 
Yes, sorry.  Typo.

However, I was mistaken.  Only Action<T> was 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Split 2
Find String 3
Park and Ride in to Central London? 2
List to CSV 3
List. CSV. Stream 2
(Collection) 1
String and Class 3
Import / Export CSV. 3

Back
Top