VB.NET split string with quotation marks in it

14,449

Solution 1

Try something like this:

Dim TestToSplit As String = "Foo"",""Bar"
Dim Splitted() As String = TestToSplit.Split(New String() {""","""}, StringSplitOptions.None)

I just tested it and got an array with Foo And Bar. I hope this helps.

Solution 2

The Split function (the way you are using it) expects a Char. If you want to split on multiple characters you need to use a string array. (Seems to me another overload of a single string value would have been handy.)

This function splits a line of text and returns an array based on the delimiter you have specified. (Of course, you could make this more general purpose by passing in the separator array.)

   Private Function SplitLine(ByVal lineOfText As String) As String()

      Dim separator() As String = {""","""}
      Dim result() As String

      result = lineOfText.Split(separator, StringSplitOptions.None)

      Return result

   End Function

Solution 3

Another alternative I often find useful is this:

Regex.Split(textToSplit, """,""")

Lets you split on more complex criteria than an array of alternative separators.

Share:
14,449
birophilo
Author by

birophilo

Just a guy/

Updated on June 14, 2022

Comments

  • birophilo
    birophilo almost 2 years

    Trying to split a line wherever "," appears (with the quotation marks). The problem is VB.NET uses " to start/end strings, so I tried using .Split(""",""") but that then splits it by " not ",".

  • birophilo
    birophilo almost 15 years
    Perfect! now all my numbers are wrong but I expected that. Works perfectly.