Home > front end >  How to remove "enters" from the text file in C#?
How to remove "enters" from the text file in C#?

Time:10-24

How can I remove all "enter" symbols from the text file in C#? Let me say I have text file with this text:

Hello

World!

I want to have "HelloWorld!"

CodePudding user response:

If you don't care about the blank or whitespace lines, you can filter them out when you read file contents:

static IEnumerable<string> ReadLines(string filePath)
{
    using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    using (var reader = new StreamReader(stream))
    {
        while (reader.ReadLine() is string dataEntry)
        {
            if (!string.IsNullOrWhiteSpace(dataEntry))
                yield return dataEntry;
        }
    }
}

Then, you could join the result into a single string as follows:

var conents = string.Join("", ReadLines("C:\\some_file.txt"));

CodePudding user response:

If you mean that you want to minify your code, I can recommend this tool. I've used it multiple times in the past and it worked flawlessly.

If you want something simpler or maybe you just want to minify one file, you can use some tools on the web like this.

  • Related