PC Review


Reply
 
 
shapper
Guest
Posts: n/a
 
      27th Jun 2008
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
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      27th Jun 2008
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<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
 
Reply With Quote
 
Jay Riggs
Guest
Posts: n/a
 
      27th Jun 2008
On Jun 26, 5:29*pm, shapper <mdmo...@gmail.com> wrote:
> 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<string> and includes:
http://www.codeproject.com/KB/database/filehelpers.aspx

-Jay

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th Jun 2008
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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      27th Jun 2008
On Jun 27, 1:52*am, Arne Vajhøj <a...@vajhoej.dk> wrote:

<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<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
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      27th Jun 2008
> 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
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      27th Jun 2008
> Just wearing my version number pedantry hat:

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

Marc
 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      27th Jun 2008
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<T> is used in
> > List<T>.ForEach since 2.0?

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use VBA to reset data validation (=list) value to first value in that list (list is a named range) ker_01 Microsoft Excel Programming 7 27th Oct 2008 03:13 PM
Compare List A to List B, Return List B Items Not in List A zwestbrook Microsoft Excel Programming 4 18th Sep 2008 10:32 PM
Global List not Display new Secruity List and Distr List =?Utf-8?B?S2V2aW4gR2Fs?= Microsoft Outlook Contacts 2 15th Aug 2006 05:02 PM
validation list--list depends on the selection of first list =?Utf-8?B?TWljaGFlbA==?= Microsoft Excel New Users 2 27th Apr 2006 10:23 PM
list 1 has 400 names List 2 has 4000. find manes from list 1 on 2 =?Utf-8?B?RWQ=?= Microsoft Excel Worksheet Functions 5 12th Sep 2005 09:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:12 AM.