简体   繁体   中英

Extra Line Breaks Detected with Visual Studio

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

如果您要问如何替换重复的新行,则传统的(懒惰和错误的)方法是:

text=text.Replace("\r\n\r\n","\r\n");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM