' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.Text
Imports System.Text.RegularExpressions

Class MainClass

    Public Shared Sub replaceandsplit(ByVal s As String)
        Dim s2 As String() = s.Replace("""", "").Split(",".ToCharArray)
        Dim i As Integer
        For i = 0 To (s2.Length - 1)
            Console.WriteLine("  " + s2(i))
        Next
    End Sub

    Public Shared Sub splitandreplace(ByVal s As String)
        Dim s2 As String() = s.Split(",".ToCharArray)
        Dim i As Integer
        For i = 0 To (s2.Length - 1)
            Console.WriteLine("  " + s2(i).Replace("""", ""))
        Next
    End Sub
    Public Shared Sub manualparse(ByVal s As String)
        Dim sb As StringBuilder = New StringBuilder 
        Dim ix As Integer = 0
        Dim inq As Boolean = False
        While ix < s.Length
            Select s.Chars(ix)
            Case """"
                If inq Then
                    If ((ix + 1) < s.Length) AndAlso (s.Chars(ix + 1) = """") Then
                        sb.Append(s.Chars(ix))
                        sb.Append(s.Chars(ix))
                        ix = ix + 1
                    Else
                        inq = False
                    End If
                Else
                    inq = True
                End If
            Case ","
                If inq Then
                    sb.Append(s.Chars(ix))
                Else
                    Console.WriteLine("  " + sb.ToString)
                    sb = New StringBuilder 
                End If
            Case Else
                sb.Append(s.Chars(ix))
            End Select 
            ix = ix + 1
        End While
        If sb.Length > 0 Then
            Console.WriteLine("  " + sb.ToString)
        End If
    End Sub
    Public Shared Sub regexparse(ByVal s As String)
        Dim res As MatchCollection = Regex.Matches(s, "([0-9]+)|([""]{1}(([^""])|(""""))*[""]{1})")
        Dim i As Integer
        For i = 0 To (res.Count - 1)
            Dim s2 As String = res(i).Value
            If s2.Chars(0) = """" Then
                Console.WriteLine("  " + s2.Substring(1, s2.Length - 2))
            Else
                Console.WriteLine("  " + s2)
            End If
        Next
    End Sub
    Private Shared Sub test(ByVal s As String)
        Console.WriteLine(s)
        Console.WriteLine("replace and split: ")
        replaceandsplit(s)
        Console.WriteLine("split and replace: ")
        splitandreplace(s)
        Console.WriteLine("manual parse: ")
        manualparse(s)
        Console.WriteLine("regex parse: ")
        regexparse(s)
    End Sub
    Public Shared Sub Main(ByVal args As String())
        test("123,""abc"",""""""quoted"""""",""partly """"quoted"""" !"",""a,b,c""")
    End Sub
End Class 
