0

I am new to VB.NET and would like to split a string into an array.

I have a string like:

613,710,200,127,127,'{\"js\":{\"\":\"16\",\"43451\":\"16\",\"65815\":\"16\",\"43452\":\"16\",\"41147\":\"16\",\"43449\":\"16\",\"43467\":\"16\",\"1249\":\"16\",\"43462\":\"16\",\"43468\":\"48\",\"43438\":\"64\",\"43439\":\"80\"}}','rca',95,2048000,3,1,'AABBCCDDEEFFGGHHIIJJKKLL=','xx.xx.xx.xx',NULL

I want to split this into a array at ",".

I tried:

Dim variable() As String
Dim stext As String

stext = "mystringhere"
variable = Split(stext, ",")

My problem is the part of

'{\"js\":{\"\":\"16\",\"43451\":\"16\",\"65815\":\"16\",\"43452\":\"16\",\"41147\":\"16\",\"43449\":\"16\",\"43467\":\"16\",\"1249\":\"16\",\"43462\":\"16\",\"43468\":\"48\",\"43438\":\"64\",\"43439\":\"80\"}}',

is split too. I want this to get all together in variable(5). Is this posible?

thank you for help

5
  • Those backslashes are very suspicious. Looks too much like what you'd see with the debugger in a C# program. Surely you are not going to type that into your program? Describe where this came from to get the help you need. Commented Sep 22, 2019 at 11:49
  • i get this strings from a text file that i want to clear out with only a few infos i need Commented Sep 22, 2019 at 13:23
  • If you open the text file in a text editor like Notepad, do you still see the backslashes (\)? Commented Sep 22, 2019 at 14:14
  • yes all lines like this 597,694,200,127,127,'{\"js\":{\"1216\":\"0\",\"\":\"16\",\"1204\":\"0\"}}','rca',60,0,0,1597,694,200,127,127,'{\"js\":{\"1216\":\"0\",\"\":\"16\",\"1204\":\"0\"}}','rca',60,0,0,1 598,695,200,127,127,'{\"js\":{\"43451\":\"16\",\"43452\":\"16\",\"41148\":\"16\",\"43431\":\"16\",\"\":\"16\",\"43110\":\"16\"}}','rca',100,0,0,1,'AABBCCDDEEFFGGHHIIJJKKLL=' this one i dont have problems 599,696,200,127,127,'{\"js\":{\"\":\"48\"}}','rca',100,0,0,1,'AABBCCDDEEFFGGHHIIJJKKLL=','xx.xx.xx.xx',NULL you see in '{\"js\":{\"\":\"48\"}}' there is no "," Commented Sep 22, 2019 at 14:24
  • This just an idea, for this case only: May be you can replace '{ and }' to be "#" first, then split the string with # as separator. Here you will get 3 split strings, replace , in split0 and split2, the char "," to be "#", and combine again these split strings to be string with insert again the '{ and }'. Now you can split your new string wih char separator of #. Commented Sep 23, 2019 at 1:41

2 Answers 2

1

What you need is a CSV parser in which you can set the field quote character. Unfortunately the TexFieldParser which comes with VB.NET doesn't have that facility. Fortunately, other ones do - here I have used the LumenWorksCsvReader, which is available as a NuGet package *.

Option Strict On
Option Infer On

Imports System.IO
Imports LumenWorks.Framework.IO.Csv

Module Module1

    Sub Main()
        Dim s = "613,710,200,127,127,'{\""js\"":{\""\"":\""16\"",\""43451\"":\""16\"",\""65815\"":\""16\"",\""43452\"":\""16\"",\""41147\"":\""16\"",\""43449\"":\""16\"",\""43467\"":\""16\"",\""1249\"":\""16\"",\""43462\"":\""16\"",\""43468\"":\""48\"",\""43438\"":\""64\"",\""43439\"":\""80\""}}','rca',95,2048000,3,1,'AABBCCDDEEFFGGHHIIJJKKLL=','xx.xx.xx.xx',NULL"

        Using sr As New StringReader(s)
            Using csvReader = New CsvReader(sr, delimiter:=","c, quote:="'"c, escape:="\"c, hasHeaders:=False)

                Dim nFields = csvReader.FieldCount

                While csvReader.ReadNextRecord()
                    For i = 0 To nFields - 1
                        Console.WriteLine(csvReader(i))
                    Next
                End While

            End Using
        End Using

        Console.ReadLine()


    End Sub

End Module

which outputs

613  
710  
200  
127  
127  
{"js":{"":"16","43451":"16","65815":"16","43452":"16","41147":"16","43449":"16","43467":"16","1249":"16","43462":"16","43468":"48","43438":"64","43439":"80"}}  
rca  
95  
2048000  
3  
1  
AABBCCDDEEFFGGHHIIJJKKLL=  
xx.xx.xx.xx  
NULL

Note that the double-quotes are doubled up in the literal string as that is the way to enter a single double-quote in VB.

If you really want the backslashes to remain, remove the escape:="\"c parameter.

If you are reading from a file then use the appropriate StreamReader instead of the StringReader.


Using the above, perhaps you have a Windows Forms program where you wanted to populate a RichTextBox with the data from, say, a text file named "C:\temp\CsvFile.txt" with the content

613,710,200,127,127,'{\""js\"":{\""\"":\""16\"",\""43451\"":\""16\"",\""65815\"":\""16\"",\""43452\"":\""16\"",\""41147\"":\""16\"",\""43449\"":\""16\"",\""43467\"":\""16\"",\""1249\"":\""16\"",\""43462\"":\""16\"",\""43468\"":\""48\"",\""43438\"":\""64\"",\""43439\"":\""80\""}}','rca',95,2048000,3,1,'AABBCCDDEEFFGGHHIIJJKKLL=','xx.xx.xx.xx',NULL
614,710,200,127,127,'{\""js\"":{\""\"":\""16\"",\""43451\"":\""16\"",\""65815\"":\""16\"",\""43452\"":\""16\"",\""41147\"":\""16\"",\""43449\"":\""16\"",\""43467\"":\""16\"",\""1249\"":\""16\"",\""43462\"":\""16\"",\""43468\"":\""48\"",\""43438\"":\""64\"",\""43439\"":\""80\""}}','din',95,2048000,3,1,'AABBCCDDEEFFGGHHIIJJKKLL=','yy.yy.yy.yy',NULL
615,710,200,127,127,'{\""js\"":{\""\"":\""16\"",\""43451\"":\""16\"",\""65815\"":\""16\"",\""43452\"":\""16\"",\""41147\"":\""16\"",\""43449\"":\""16\"",\""43467\"":\""16\"",\""1249\"":\""16\"",\""43462\"":\""16\"",\""43468\"":\""48\"",\""43438\"":\""64\"",\""43439\"":\""80\""}}','jst',95,2048000,3,1,'AABBCCDDEEFFGGHHIIJJKKLL=','zz.zz.zz.zz',NULL

you could use the above to come up with

Imports System.IO
Imports LumenWorks.Framework.IO.Csv

Public Class Form1

    Public Class Datum
        Property A As Integer
        Property B As Integer
        Property C As Integer
        Property D As Integer
        Property E As Integer
        Property JsonData As String
        Property SocketType As String
        Property F As Integer
        Property G As Integer
        Property H As Integer
        Property I As Integer
        Property Base64Data As String
        Property IpAddy As String
        Property J As String

        Public Overrides Function ToString() As String
            Return $"{A}, {SocketType}, {IpAddy}, {B} ,{C}, {D}, {E}, {F}, {G}, {H}, {I}, {JsonData}, {Base64Data}, {J}"
        End Function

    End Class

    Public Function GetData(filename As String) As List(Of Datum)
        Dim data As New List(Of Datum)

        Using sr As New StreamReader(filename)
            Using csvReader = New CsvReader(sr, hasHeaders:=False, delimiter:=","c, quote:="'"c, escape:="\"c, comment:=Nothing, trimmingOptions:=ValueTrimmingOptions.UnquotedOnly)

                Dim nFields = csvReader.FieldCount
                If nFields <> 14 Then
                    Throw New MalformedCsvException("Did not find 14 fields in the file " & filename)
                End If

                While csvReader.ReadNextRecord()
                    Dim d As New Datum()
                    d.A = Integer.Parse(csvReader(0))
                    d.B = Integer.Parse(csvReader(1))
                    d.C = Integer.Parse(csvReader(2))
                    d.D = Integer.Parse(csvReader(3))
                    d.E = Integer.Parse(csvReader(4))
                    d.JsonData = csvReader(5)
                    d.SocketType = csvReader(6)
                    d.F = Integer.Parse(csvReader(7))
                    d.G = Integer.Parse(csvReader(8))
                    d.H = Integer.Parse(csvReader(9))
                    d.I = Integer.Parse(csvReader(10))
                    d.Base64Data = csvReader(11)
                    d.IpAddy = csvReader(12)
                    d.J = csvReader(13)

                    data.Add(d)

                End While

            End Using
        End Using

        Return data

    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim srcFile = "C:\temp\CsvData.txt"
        Dim dat = GetData(srcFile)

        For Each d In dat
            RichTextBox1.AppendText(d.ToString() & vbCrLf)
        Next

    End Sub

End Class

It might be necessary to perform more checks on the data when trying to parse it. Note that I made a function for the .ToString() method of the Datum class and put the properties in a different order just to demonstrate its use.


* Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution... Choose the "Browse" tab -> type in LumenWorksCsvReader -> select the one by Sébastien Lorion et al., -> tick your project name in the pane to the right -> click Install.

Sign up to request clarification or add additional context in comments.

Comments

0

I am new to VB.NET and would like to split a string into an array. ... variable = Split(stext,",")

Instead of

variable = Split(stext,",")

use

variable = stext.split(",")

If you want to get a bit more complicated on your split you would create an array of char data as such

dim data(3) as char
data(0) = ","c
data(1) = vbcrlf
data(2) = chr(34)
data(3) = vbtab
... and so on
variable = stext.split(data)

3 Comments

Please explain why you should use stext.Split instead of Split(stext,",").
Your code will not compile with Option Strict On. Option Strict should almost always be On.
Split(str,",") is a function of the Microsoft.VisualBasic.Strings Module with passing the 'WhatToSplit' argument too. str.split(",") is a Function of the System.String class itself. For .net, the latter is the more 'native' syntax. Both do more or less the same, althought the String.Split has multiple overloads for passing different type of arguments.So as long in VB, and splitting by a string-separator, there is no real difference from your perspective.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.