c# - how to remove spaces and identify multiple spaces and place them in a table data from text file? -
i have huge text tables in text file , able read data inside winform application there irregular data coming inside datagrid. needed table is, datagrid. want catch "*( 3)" data , using number want split data below. plate id column can have n number of id's. according plate id's x-coord , y-coord's added them. how place table data inside datagrid without disturbing it's structure? need re-use data datagrid again.
*( 3)car plate coordinates: * no. plate plate no. x-coord y-coord * plate type id coord (in) (in) 2 'ca' 1 5 8.6250 -23.3750 32.6249 -23.3750 46.5983 120.6250 46.5983 120.6250 8.6250 120.6250 'ca' 2 5 8.6250 120.6250 46.5983 120.6250 64.6250 306.3959 59.3717 369.4359 8.6250 365.2070 'ca' 3 5 8.6250 120.6250 46.5983 120.6250 64.6250 306.3959 59.3717 369.4359 8.6250 365.2070
lbl2.text = fdlg.filename; ienumerable<string> lines = file.readlines(lbl2.text); if (lines.count() > 0 && lines.contains("*( 3)")) { foreach (var columnname in lines.firstordefault().split(new[] { ' ' }, stringsplitoptions.removeemptyentries)) { datagridview3.columns.add(columnname, columnname); while (lines.contains(null)) break; foreach (var cellvalues in lines.skip(1)) { var cellarray = cellvalues.split(new[] { ' ' }, stringsplitoptions.removeemptyentries); while (lines.contains("*(")) { foreach (var columnname in lines.firstordefault() .split(new[] { ' ' }, stringsplitoptions.removeemptyentries)) datagridview3.columns.add(columnname, columnname); } } } }
try following code uses fixed width columns instead of splitting columns
using system; using system.collections.generic; using system.linq; using system.text; using system.io; using system.data; namespace consoleapplication49 { class program { const string filename = @"c:\temp\test.txt"; static int[] column_widths = { 9, 7, 7, 7, 9, 9}; static void main(string[] args) { datatable dt = new datatable(); dt.columns.add("no. plate", typeof(int)); dt.columns.add("plate type", typeof(string)); dt.columns.add("plate id", typeof(int)); dt.columns.add("no. coord", typeof(int)); dt.columns.add("x-coord", typeof(double)); dt.columns.add("y-coord", typeof(double)); int noplate = 0; string platetype = ""; int plateid = 0; int nocoord = 0; streamreader reader = new streamreader(filename); string inputline = ""; while ((inputline = reader.readline()) != null) { if (!inputline.startswith("*")) { string[] splitarray = fixedcolumns(inputline); datarow newrow = dt.rows.add(); if (splitarray[0].trim().length > 0) { noplate = int.parse(splitarray[0]); } newrow["no. plate"] = noplate; if (splitarray[1].trim().length > 0) { platetype = splitarray[1]; } newrow["plate type"] = platetype; if (splitarray[2].trim().length > 0) { plateid = int.parse(splitarray[2]); } newrow["plate id"] = plateid; if (splitarray[3].trim().length > 0) { nocoord = int.parse(splitarray[3]); } newrow["no. coord"] = nocoord; newrow["x-coord"] = double.parse(splitarray[4]); newrow["y-coord"] = double.parse(splitarray[5]); } } } static string[] fixedcolumns(string input) { string[] splitarray = new string[column_widths.length]; int startcol = 0; (int = 0; < column_widths.length; i++) { if (i < column_widths.length - 1) { splitarray[i] = input.substring(startcol, column_widths[i]).trim(); } else { splitarray[i] = input.substring(startcol).trim(); } startcol += column_widths[i]; } return splitarray; } } }
Comments
Post a Comment