查找空格并在文本文件C#上添加换行符

时间:2017-10-31 09:24:15

标签: c#

您好想从此

转换此文件
KES 32265   5000001 1   10  COPY    05

KES 32265   5000001 1   10  PRINT   05

KES 32265   5000001 1   30  PRINT   05

KES 32265   5000001 10  100 PRINT   05

对此:

KES
100317
6114488
1
-
10.00
KPC1
-
-
5
-

KES
100317
6114488
1
-
30.00
KPC3
-
-
05
-

在使用C#的文本文件中这是我的代码。

  static void Main(string[] args)
    {
        //DirectoryInfo directoryInfo = new DirectoryInfo(@"D:\DestinationFolder");
        //if the director exists then proceed

        FileStream inFile = new FileStream(@"C:\x\KEN.txt", FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(inFile);
        string record;
        string input = Console.ReadLine();
        try
        {
            //the program reads the record and displays it on the screen
            record = reader.ReadLine();
            while (record != null)
            {
                //if (record.Contains(" "))
                //{
                //    Console.WriteLine(record);
                //}
                //record = reader.ReadLine();
                ConvertWhitespaceToSpacesString(record);
            }
        }
        finally
        {
            //after the record is done being read, the progam closes
            reader.Close();
            inFile.Close();
        }
        Console.ReadLine();
    }

    public static string ConvertWhitespaceToSpacesString(string value)
    {
        value = value.Replace('\r', ' ');
        value = value.Replace('\n', ' ');
        return value;
    }

2 个答案:

答案 0 :(得分:0)

这应该有效。我使用字符串拆分方法来删除空格。 :

        try
        {
            //the program reads the record and displays it on the screen
            record = reader.ReadLine();
            while (record != null)
            {
                string[] array = record.Split(new char[] {' ', '\r', '\n', '\t'}, StringSplitOptions.RemoveEmptyEntries).ToArray();
                foreach(string word in array)
                {
                    Console.WriteLine(word);
                }

            }
        }

答案 1 :(得分:0)

你不清楚你在问什么,如果你想让输出在文件中,你可以尝试这个FormatFile函数。它虽然一次从文件中读取所有行,所以要小心更大的文件。它循环遍历所有行,然后它将该行拆分为空白字符。根据您想要的输出看起来,您可以更改“if”语句。最后将StreamWriter刷新到Filestream。所有关闭和处理都由using语句完成。

static void Main(string[] args)
{
    FormatFile(@"c:\test.txt");
}

public static void FormatFile(string pathToFile)
{
    string[] lines = File.ReadAllLines(pathToFile);
    using (FileStream fs = new FileStream(pathToFile, FileMode.OpenOrCreate))
    using (StreamWriter sw = new StreamWriter(fs))
    {
        foreach( string line in lines)
        {
            if (String.IsNullOrWhiteSpace(line))
                continue;

            string[] parts = line.Split(' ');

            for (int i = 0; i < parts.Length; i++)
            {
                string part = parts[i].Trim();
                if (!String.IsNullOrWhiteSpace(part))
                {
                    sw.WriteLine(part);
                }
                else //if( ... )
                {
                    sw.WriteLine("-");
                }
                //else if( ...)
                //{
                //    sw.WriteLine("KPC1");
                //}
                //else
                //{
                //
                //}
            }   
        }
        sw.Flush(); 
    }
}