如何从给定的字符串中拆分每个列值

How to split each column value from the given string

我正在尝试从字符串变量中拆分每个列值。该值来自 excel 逗号分隔文件。我怎样才能从每个字符串中拆分每一列

 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\""
//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("\",\"", "|");

此程序拆分您的字符串并将每一列写入其自己的行中:

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);
    }
  }
}

在这里测试:https://dotnetfiddle.net/0C8nLP

输出:
1
卡车
FN60HZU
'厕所

26/03/2022
H2
外侧外侧
1
31580225
三角形
TRS02
14
14
14

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

我创建了使用 Regex 相应地拆分 csv 行的函数。

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

        _ReturnThis = rexCsvSplitter.Split(fReadLine);

        return _ReturnThis;
    }