4

Possible Duplicate:
Parse Delimited CSV in .NET

Hi,

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

Community
  • 1
  • 1
chobo
  • 31,561
  • 38
  • 123
  • 191
  • Did you Google this before you asked it? – Devin Burke Apr 08 '11 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. –  Apr 08 '11 at 17:04
  • 3
    See [SO: Parse Delimited CSV in .NET](http://stackoverflow.com/questions/736629/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](http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx) can be given a stream and a stream can be created directly from a string with [StreamReader](http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx) The first reply in the post also shows a "safer" method of manual parsing. –  Apr 08 '11 at 17:08

3 Answers3

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!

DWRoelands
  • 4,878
  • 4
  • 29
  • 42
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

thorkia
  • 1,972
  • 1
  • 20
  • 26
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

Angelom
  • 2,413
  • 1
  • 15
  • 8