public class myRows
{
public decimal Number1 { get; set; }
public decimal Number2 { get; set; }
public decimal Number3 { get; set; }
public decimal Number4 { get; set; }
public decimal Number5 { get; set; }
public decimal Number6 { get; set; }
public string Date1 { get; set; }
public myRows(string str)
{
Number1 = Convert.ToDecimal(str.Substring(3, 7));
Number2 = Convert.ToDecimal(str.Substring(15, 8));
Number3 = Convert.ToDecimal(str.Substring(24, 8));
Number4 = Convert.ToDecimal(str.Substring(36, 8));
Number5 = Convert.ToDecimal(str.Substring(47, 8));
Number6 = Convert.ToDecimal(str.Substring(58, 8));
Date1 = str.Substring(65, 25);
}
}
i then read the text file data like
var myRows = new List<myRows>();
var myR = new StreamReader(txtFileToImport.Text);
while (!myR.EndOfStream)
{
string s = myR.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
myRows.Add(new myRows(s));
}
}
myR.Close();
dataGridView1.DataSource = myRows;
The problem am having is pre-determining the startIndex and length of the white space between the column values in the text file e.g here Convert.ToDecimal(str.Substring(3, 7));
White space between column values isn’t uniform, it can be 5 between column 1 and 2 and then be 8 between column 7 and 8.
Currently, i have to know in advance the index at which the white space starts and ends. Is there away i can dynamically get the start Index of the white space and its length with out getting to look at the text file to be processed?
What i really need is the parameters passed to str.Substring(,) not to be hard corded.
@Habib: sample text file is here

Use
String.Spliton the space character with theStringSplitOptions.RemoveEmptyEntriesoption. This will remove the empty fields effectively leaving only fields with your data.Given a line of your text file in
line, your code would look something like this:Since your date data is broken up into several descrete fields (day of week, month, day, etc) you’ll ‘reassemble’ them through the fields they’ll occupy and assign to your
Date1:Of course in production code you’ll want to validate a couple things:
use
Double.TryParse)