Home > OS >  How to split each column value from the given string
How to split each column value from the given string

Time:05-09

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\""

CodePudding user response:

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

using System;

class Program {
  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\",\"5TST001\",\"16:01:00\",\"16:07:16\",\"40:D3:AE:CB:16:EE\"";

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

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

You can test it here: https://replit.com/languages/csharp

CodePudding user response:

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