我正在尝试将每个列值从字符串变量中分割出来。该值来自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\""发布于 2022-05-09 08:19:27
//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("\",\"", "|");发布于 2022-05-09 08:24:54
此程序拆分字符串并将每一列写入其自己的行中:
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
“'WC”
2022年3月26日
H2
左侧外
1
31580225
三角
TRS02
14
14
14
17,18,19
5TST001
下午16:01:00
16:07:16
40:D3:AE:CB:16:EE
https://stackoverflow.com/questions/72168129
复制相似问题