Hi,
I have large string. I want to parse it by delimeter and input to datatable. All lines in string are ended by "\n" (vbcrlf) character. The delimeter is specified by user (like comma, tab, space or something else).
So how can I do this?
Thank's Alexei |
| Alexei_shk Thursday, July 13, 2006 9:03 AM |
to split a string, you can use string.Split method, for example:
string names = "John,Bill,Joy,Andy" ;
string[] elems = names.Split ( new char[] { ',' } ) ;
How you want your datatable like ?
hope this helps |
| Andoko Thursday, July 13, 2006 11:05 AM |
Hi,
I want get the datatable with columns as maximum of tokens in the lines. I can scan all lines and get the maximum number of columns and after itscan the lines in secondand insert all data. But it is easily slow. I need another solution.
Thank's Alexei |
| Alexei_shk Thursday, July 13, 2006 11:35 AM |
Can you provide some example of the strings and DataTable that you want ??? And also your current solution will be good
Thanks |
| Andoko Thursday, July 13, 2006 11:46 AM |
Hi,
My data in string is like: 1,fsd,dsf34,dfsf,....\n 2,sdf,sdfsf324,sfs,sdfsf,sfs....\n ..... Number of columns is not same in all rows!
The data must be parsed to datatable quickly. Columns in datatable must be: Column1, Column2, Column3,...
Thank's Alexei |
| Alexei_shk Thursday, July 13, 2006 1:54 PM |
can you try this;
define and create a datatable
while parsing string { get the line and remove '\n' split the string
create a row
loop thru the string array { if column[index] is null then insert a new column to the datatable assign the string to the current column } } |
| JRQ Thursday, July 13, 2006 2:59 PM |
Hi,
It's nice but it doesn't work becouse you can't add columns to datatable in .Net 1.1. I have tryed but got the error that to table with values can be added new column.
Thank's Alexei |
| Alexei_shk Sunday, July 16, 2006 6:41 AM |
Actually, you can add column to a DataTable using DataTable.Columns.Add method, for example
DataTable dt = new DataTable ( ) ;
dt.Columns.Add ( "Name", typeof ( string ) ) ;
dt.Columns.Add ( "Age", typeof ( int ) ) ;
dt.Rows.Add ( new object[] { "John", 25 } ) ;
dt.Rows.Add ( new object[] { "Mary", 21 } ) ;
hope that helps |
| Andoko Sunday, July 16, 2006 5:48 PM |
Hi,
Try to read before. I know how to add columns!!! But I can't add columns when I have rows in datatable. This is my problem.
Thank's Alexei |
| Alexei_shk Monday, July 17, 2006 5:51 AM |
I haven't actually done a column drop/remove. But is it possible to just define a sufficient max number of columns then drop the columns that you don't need after fill. |
| JRQ Monday, July 17, 2006 4:39 PM |