简体   繁体   English

在C#中使用streamReader类

[英]using streamReader class in c#

  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 eg here Convert.ToDecimal(str.Substring(3, 7)); 问题是预先确定文本文件中列值之间的startIndex和空白长度,例如,在此处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. 列值之间的空白不一致,在第1列和第2列之间可以为5,然后在第7列和第8列之间可以为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. 我真正需要的是传递给str.Substring(,)的参数str.Substring(,)不要太难了。

@Habib: sample text file is here @Habib: 示例文本文件在这里 在此处输入图片说明

You can use string.Split( ) on your string and later use the array index rather than string positions to convert the numbers. 您可以在string上使用string.Split( ),然后再使用数组索引而不是字符串位置来转换数字。 Splitting the string on white space will remove the white space from the string. 在空格上分割字符串会从字符串中删除空格。 Something like: 就像是:

public myRows(string str)
       {
          string[] splitArray = str.Split(); //split on white space
          if(splitArray.Length > 7)
              {
               Number1 = Convert.ToDecimal(splitArray[0]);
               //,....... So on
              }
       }

Use String.Split on the space character with the StringSplitOptions.RemoveEmptyEntries option. 通过StringSplitOptions.RemoveEmptyEntries选项在空格字符上使用String.Split 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: 由于在文本文件中的一行line ,您的代码会是这个样子:

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 : 由于您的日期数据分为几个离散字段(星期几,月,日等),因此您将通过它们占据的字段“重新组合”它们并分配给您的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 ) 您可以安全地将字符串字段值转换为Doubles(为此使用Double.TryParse

using Split method split your line into 8 parts. 使用Split方法将您的线分成8部分。 Last one will be your date. 最后一个将是您的约会日期。

var parts = line.Split(new char[]{' '}, 8, StringSplitOptions.RemoveEmptyEntries);

Number1 = Decimal.Parse(parts[0]);
Number2 = Decimal.Parse(parts[1]);
......
......
DateTime Date1 = DateTime.ParseExact(parts[7],"ddd MMM dd HH:mm:ss yyyy",CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM