如题

解决方案 »

  1.   

    String的Trim()方法不就是却除掉两端的空格的吗?
      

  2.   


         string strTemp = string.Empty;
    ......
     FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read);
    StreamReader read = new StreamReader(fs);
    while (read.Peek() > -1)
    {
    strTemp = read.ReadLine().Trim();// 去掉所有空格
    ......
    StreamWriter sw;
    sw = File.AppendText(FileExportPath);
    sw.WriteLine(strTemp );
    sw.Flush();
    sw.Close();
    } read.Close();
      

  3.   

    string str=" a a a a";
    string str1=str.Trim();
      

  4.   

    String的Trim()方法不就是却除掉两端的空格的吗是去掉所有的空格如果只需要去掉首尾的,你可以读出来去掉后再写进去
      

  5.   


    public static void ReplaceSpace()
            {
                String fileName = "c:\\forTest.txt";
                String[] lines = File.ReadAllLines(fileName);
                if(lines.Length>0)
                {
                    lines[0] = lines[0].TrimStart();
                    lines[lines.Length - 1] = lines[lines.Length - 1].TrimEnd();
                }
                File.WriteAllLines(fileName,lines);
            }虽然这种效率不高,不过也可以实现,如果是大文件的话,还是用流的方式吧
      

  6.   

    文件取出的内容Trim()就可以,Trim()会取出头尾的空白符(包括空格、换行符等)
      

  7.   


                string path = "D:\\文件路径.txt";
                // 创建文件流
                try
                {
                    StreamReader sr = new StreamReader(path);
                    string content = sr.ReadToEnd().Trim();
                    StreamWriter sw = new StreamWriter(path + ".txt");
                    sw.Write(content);
                    sw.Dispose();
                    sw.Close(); // 关闭流
                    sr.Dispose();
                    sr.Close(); // 关闭流
                }
                catch
                {
                    
                }
      

  8.   

    可以改进为对同一个TXT文件,读取一行后,去除两端的空格后,立即更新到该文件的该行吗?
      

  9.   


     string path = "D:\\文件路径.txt";
                // 创建文件流
                try
                {
                    StreamReader sr = new StreamReader(path);
                    string content = sr.ReadToEnd().Trim();
                    StreamWriter sw = new StreamWriter(path);
                    sr.Dispose();
                    sr.Close(); // 关闭流                sw.Write(content);
                    sw.Dispose();
                    sw.Close(); // 关闭流
                                }
                catch
                {
                    
                }
    OK.