1

While I read my txt file in visual studio, its shows extra line breaks, but when I open same file with notepad it doesn't show those line breaks.

I am reading my file with System.IO.File

var file = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "myfile.txt")

How to read file without those unwanted linefeeds. Don't want to do replace .replace("\r\n","") since it will replace all genuine line feed as well.

EDit Just Tried with NotePad++ With Notepad ++

enter image description here

Now with Normal Notepad

enter image description here

10K35H 5H4KY4
  • 1,496
  • 5
  • 19
  • 41

2 Answers2

0

If you're asking how to replace repeated new lines, the traditional (and lazy and wrong) way to do it is:

text=text.Replace("\r\n\r\n","\r\n");
Blindy
  • 65,249
  • 10
  • 91
  • 131
0

Your Notepad++ screenshot shows the problem very clearly. You have multiple line breaks at the end of certain lines.(Well blank lines in other words).

 var alllines =  File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory +  "myfile.txt")

 var validlines = alllines.Where(l=>!string.IsNullOrEmpty(l));

now use validlines in your code instead of all lines

Kosala W
  • 2,133
  • 1
  • 15
  • 20