2
FASTER,WW0011,"CTR ,REURN,ALT TUBING HELIUM LEAK",DEFAULT test,1,3.81,test

I need to get the result of the following line as

Arr(0) =faster
Arr(1) =WW0011
Arr(2) =CTR ,REURN,ALT TUBING HELIUM LEAK
Arr(3) =DEFAULT test
Arr(4) =faster
Arr(5) = 1
Arr(6)=3.81
Arr(7) = test

I tried using split, but the problem is on Arr(2) could anyone please give me a solution

har07
  • 88,338
  • 12
  • 84
  • 137
Ruban J
  • 622
  • 1
  • 7
  • 31

3 Answers3

4

You could use the TextFieldParser class which will take care of situations like this. Set the HasFieldEnclosedInQuotes property to true. Here is an example from MSDN (slightly altered):

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\logs\bigfile")

    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {","}

    'Set this to ignore commas in quoted fields.
    MyReader.HasFieldsEnclosedInQuotes = True

    Dim currentRow As String()
    'Loop through all of the fields in the file.  
    'If any lines are corrupt, report an error and continue parsing.  
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            ' Include code here to handle the row. 
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & " is invalid.  Skipping")
        End Try 
    End While 
End Using
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
3

I use this function alot myself

Private Function splitQuoted(ByVal line As String, ByVal delimeter As Char) As String()
Dim list As New List(Of String)

    Do While line.IndexOf(delimeter) <> -1
        If line.StartsWith("""") Then
            line = line.Substring(1)
            Dim idx As Integer = line.IndexOf("""")
            While line.IndexOf("""", idx) = line.IndexOf("""""", idx)
                idx = line.IndexOf("""""", idx) + 2
            End While
            idx = line.IndexOf("""", idx)
            list.Add(line.Substring(0, idx))
            line = line.Substring(idx + 2)
        Else
            list.Add(line.Substring(0, Math.Max(line.IndexOf(delimeter), 0)))
            line = line.Substring(line.IndexOf(delimeter) + 1)
        End If
    Loop
    list.Add(line)
    Return list.ToArray
End Function
Keith Mifsud
  • 725
  • 5
  • 16
-1

Use a for loop to iterate the string char by char!

Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
Kenneth Li
  • 1,632
  • 1
  • 14
  • 19