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

C# 在文本文件C上查找空白并添加换行符#,c#,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.0

嗨,我想从这个文件转换这个文件

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;
    }

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

        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);
                }

            }
        }

现在还不太清楚您在问什么,如果您希望输出也在文件中,您可以尝试这个FormatFile函数。但它会一次读取文件中的所有行,所以要小心较大的文件。它循环遍历所有行,然后在空白字符上拆分该行。根据您希望输出的外观,您可以更改“if”语句。最后,将StreamWriter向下刷新到文件流。所有关闭和处理都由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(); 
    }
}
static void Main(字符串[]args)
{
格式文件(@“c:\test.txt”);
}
公共静态void格式文件(字符串pathToFile)
{
string[]lines=File.ReadAllLines(pathToFile);
使用(FileStream fs=newfilestream(pathToFile,FileMode.OpenOrCreate))
使用(StreamWriter sw=新StreamWriter(fs))
{
foreach(行中的字符串行)
{
if(String.IsNullOrWhiteSpace(行))
持续
字符串[]部分=行分割(“”);
对于(int i=0;i
除非您尝试过,否则我们无法帮助您,请发布您的代码。我看不到您的输入和输出之间的连接。如果要将空白更改为新行,只需使用
Replace
str=str.Replace(“,”\n”)输入和输出不匹配,是吗?