Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8946255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:27:49+00:00 2026-06-15T12:27:49+00:00

public class myRows { public decimal Number1 { get; set; } public decimal Number2

  • 0
  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
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:27:50+00:00Added an answer on June 15, 2026 at 12:27 pm

    Use String.Split on the space character with the StringSplitOptions.RemoveEmptyEntries option. 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:

    var fields = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    var Number1 = Convert.ToDecimal(fields[0]);
    var Number2 = Convert.ToDecimal(fields[1]);
    // etc.
    

    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:

    var Date1 = string.Format("{0} {1} {2} {3} {4}", fields[7], fields[8], fields[9], fields[10], fields[11]);
    

    Of course in production code you’ll want to validate a couple things:

    • You’ve read the expected number of fields
    • That you can safely convert your string field values to Doubles (for this
      use Double.TryParse)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class myRows { public decimal Col1 { get; set; } public decimal Col2
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Emp { public int ID { get; set; } public string Name
public class Abbreviation { public string ShortName { get; set; } public string LongName
public class User { public long Id {get;set;} [References(typeof(City))] public long CityId {get;set;} [????]
public class Base{ protected String str; public static final Base ERROR = new Base(error);
public class SearchForm { //Note: Property is nullable public DateTime? CurrentViewDate {get;set;} public DateTime
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.