日期:2014-05-18  浏览次数:20878 次

求高手解惑
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication9
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
  int j=0;
  string a = "12051220.003";
  StreamReader myReader = new StreamReader(@"D:\"+a, Encoding.GetEncoding("GB2312"));
  string myline = " ";
  myline = myReader.ReadLine();
   
  while ((myline = myReader.ReadLine()) != null)
  {
  j = j + 1;
  myline.Replace(" "," ");
  myline.Replace(" "," ");
  myline.Replace(" "," ");
  string[] sArray = myline.Split(' ');
  if (j==4)
  { 
   
  richTextBox1.AppendText(sArray[6] + "\r\n");
  myWriter.Close();
  return;  
   
  }  

  }

  myReader.Close();

  }
  }
   
}
有两个问题第一个就是ReadLine()只能从第二行开始读,第一行被隔过去了
第二个问题是我想用myline.Replace(" "," ");
  myline.Replace(" "," ");
  myline.Replace(" "," ");
这段代码来实现将几个空格换成一个空格的目第,但是实现不了
举例  
  1 1 1 1 1
我想把上边的一行变为下边这样的格式
  1 1 1 1 1

------解决方案--------------------
myline = myReader.ReadLine();
while ((myline = myReader.ReadLine()) != null)

因为你ReadLine了两次
------解决方案--------------------
C# code
while (myline.IndexOf("  ") >= 0)
{
    myline = myline.Replace("  ", " ");
}