4

Possible Duplicate:
Parse Delimited CSV in .NET

Hi,

How can I convert a csv string into a list in vb.net?

3
  • Did you Google this before you asked it? Apr 8, 2011 at 17:04
  • 1
    Depends on the "type" of CSV string. For a very primitive string without embedded separators or quotes, string.split will work. However, this is not an answer because it's about as "correct" as using a regex to parse HTML. I am pretty sure the VisualBasic namespace contains support for CSV file/stream parsing, so that would be a place to look.
    – user166390
    Apr 8, 2011 at 17:04
  • 3
    See SO: Parse Delimited CSV in .NET It's not an exact duplicate, because that deals with a file, but the same approach of the 2nd answer which uses TextFieldParser can be given a stream and a stream can be created directly from a string with StreamReader The first reply in the post also shows a "safer" method of manual parsing.
    – user166390
    Apr 8, 2011 at 17:08

3 Answers 3

14

You want something like this...

Dim MyString as String = "one,two,three,four,five"
Dim MyArray() as String = Mystring.Split(",")
Dim MyList as List(Of String) = MyArray.ToList()

Hope this helps!

4
  • 3
    Breaks for "one,two",two,three
    – user166390
    Apr 8, 2011 at 17:09
  • 2
    On problem, I am getting an error saying Array.tolist() is not a member of system.array. I am using .net framework 4.0
    – chobo
    Apr 8, 2011 at 17:13
  • This is Helpful Dec 20, 2018 at 7:50
  • @chobo or anybody reading this, One need to import System.LinQ
    – Nandostyle
    Dec 19, 2021 at 23:48
4

using Linq:

Dim parts As String = "1,2,3,4"
Dim itemsList As List(Of String)

itemsList = (From s In parts.Split(",")
            Select s).ToList()

Gives you a list with 4 items

1

Take a look at the split function.

    Dim a As String
    a = "data1,2,3,4,5"

    Dim b() As String
    b = a.Split(",")

    Dim c As List(Of String)
    c = b.ToList()

c will be a list of strings

2
  • 5
    Breaks for "one,two",two,three
    – user166390
    Apr 8, 2011 at 17:09
  • You may code like this too: Dim a As String a = "data1,2,3,4,5" Dim c As List(Of String) c = a.Split(",").ToList()
    – 8oris
    Aug 9, 2021 at 14:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.