VB.NET split string with quotation marks in it
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.
Comments
-
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 almost 15 yearsPerfect! now all my numbers are wrong but I expected that. Works perfectly.