Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

 
Share this answer
 
Use the Microsoft.VisualBasic.FileIO.TextFieldParser class:

C#
TextFieldParser parser = new TextFieldParser(new StringReader(csvAsString));

// or read from the file
// TextFieldParser parser = new TextFieldParser("mycsvfile.csv");

parser.HasFieldsEnclosedInQuotes = true;
parser.SetDelimiters(",");

string[] splitStrings;

while (!parser.EndOfData)
{
    fields = parser.ReadFields();
    foreach (splitString in splitStrings)
    {
        Console.WriteLine(splitString);
    }
} 
//Don't forget cleanup
parser.Close();
 
Share this answer
 
v2
VB
Private Function splitQuotedLine(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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900