polachan paily
polachan paily

Reputation: 69

How to split each column value from the given string

I am trying to split each column value from the string variable. The value is coming from an excel comma seperated file. how can I split each column from each string

 string1=    "\"1\",\"Truck\",\"FN60HZU\",\"'WC\",,\"26/03/2022\",\"H2\",\"NEARSIDE OUTER\",\"1\",\"31580225\",\"TRIANGLE\",\"TRS02\",\"14\",\"14\",\"14\",,\"17\",\"5TST001\",\"16:01:00\",\"16:07:16\",\"40:D3:AE:CB:16:EE\""
  string2 =   "\"1\",\"Truck\",\"FN60HZU\",\"'WC\",,\"26/03/2022\",\"H2\",\"OFFSIDE OUTER\",\"2\",\"29580225\",\"FROMWAY\",\"HD919\",\"15\",\"15\",\"15\",,\"17\",\"5TST001\",\"16:01:00\",\"16:07:16\",\"40:D3:AE:CB:16:EE\""
 string3=   "\"1\",\"Truck\",\"FN60HZU\",\"'WC\",,\"26/03/2022\",\"H2\",\"NEARSIDE INNER\",\"2\",\"29580225\",\"GOODYEAR\",\"KMAXD\",\"12\",\"12\",\"12\",,\"17\",\"5TST001\",\"16:01:00\",\"16:07:16\",\"40:D3:AE:CB:16:EE\""

Upvotes: 0

Views: 525

Answers (3)

Devas1900
Devas1900

Reputation: 21

I created function which I used Regex to split csv line accordingly.

public static string[] CSVReadlineToArray(this string fReadLine)
    {
        string[] _ReturnThis = null;
        Regex rexCsvSplitter = new Regex(@",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))");

        _ReturnThis = rexCsvSplitter.Split(fReadLine);

        return _ReturnThis;
    }

Upvotes: 2

Svein Terje Gaup
Svein Terje Gaup

Reputation: 1578

This program splits your string and writes each column in its own row:

using System;
class Program {
  public static void Main(string[] args) {
    var string1="\"1\",\"Truck\",\"FN60HZU\",\"'WC\",,\"26/03/2022\",\"H2\",\"NEARSIDE OUTER\",\"1\",\"31580225\",\"TRIANGLE\",\"TRS02\",\"14\",\"14\",\"14\",,\"17,18,19\",\"5TST001\",\"16:01:00\",\"16:07:16\",\"40:D3:AE:CB:16:EE\"";

    var columns = string1.Replace("\",\"", "|").Replace(",,", "||").Replace("\"","").Split('|');

    foreach(var col in columns)
    {
       Console.WriteLine(col);
    }
  }
}

Test it here: https://dotnetfiddle.net/0C8nLP

Output:
1
Truck
FN60HZU
'WC

26/03/2022
H2
NEARSIDE OUTER
1
31580225
TRIANGLE
TRS02
14
14
14

17,18,19
5TST001
16:01:00
16:07:16
40:D3:AE:CB:16:EE

Upvotes: 0

bakit ko pinasok to
bakit ko pinasok to

Reputation: 61

//I know this is a dumb answer (?) lmao, but I've experienced like this before.
        //Just simply replace the current delimiter into new delimiter then split with your new delimiter.
        //This one works for me.

        yourstring.Replace("\",\"", "|");

Upvotes: 0

Related Questions