換行符 (newline character(s))


問題描述

換行符 (newline character(s))

您的軟件是否處理來自其他系統的換行符?

Linux/BSD    linefeed         ^J    10     x0A
Windows/IBM  return linefeed  ^M^J  13 10  x0D x0A
old Macs     return           ^M    13     x0D
others?

出於精神錯亂的原因,我將在我的文本文件中使用 Linux 版本的換行符。但是,當我把我的文本文件帶到 Windows 時,有些程序不能很好地處理我的文本中的換行符。你會怎麼處理呢?


參考解法

方法 1:

As they say, be strict in what you write and liberal in what you read.

Your application should be able to work properly reading both line endings. If you want to use linefeeds, and potentially upset Windows users, that's fine.

But save for Notepad, most programs I play with seem to be happy with both methods.

(And I use Cygwin on Windows, which just makes everything interesting)

方法 2:

The standard Python distribution comes with two command‑line scripts (in Tools/scripts) called crlf.py and lfcr.py that can convert between Windows and Unix/Linux line endings.

[Source]

方法 3:

In .NET, new lines are denoted by Environment.NewLine, so the framework is designed in such a way as to take whatever the system's new line is (CR+LF or CR only or LF only) to use at runtime. Of course this is ultimately useful in Mono.

方法 4:

I suspect you will find that most modern Windows programs (with the notable exception of Notepad) handle newline‑only files just fine. However, files generated with windows programs still tend to have crlf endings.

Most of the time, the line endings will automatically be handled in a platform‑specific way by the runtime library. For example, a C program that opens a file with fopen(..., "r") will see the lines in a consistent way (linefeed only) on any platform regardless of the actual line endings.

方法 5:

As far as I know, it's only Notepad that has a problem with line separators. Virtually ever other piece of software in the world accepts any of those three types of separator, and possibility others as well. Unfortunately, Notepad is the editor of first resort for most computer users these days. I think it's extremely irresponsible of Microsoft to let this situation continue. I've never played with Vista, but I believe the problem still exists there, as it does in XP. Any body know about the next version?

(by Mark StockWill HartungBen HoffsteinJon LimjapGreg HewgillAlan Moore)

參考文件

  1. newline character(s) (CC BY‑SA 2.5/3.0/4.0)

#newline #encoding #text






相關問題

如何格式化電子郵件中的字符串以便 Outlook 打印換行符? (How do I format a String in an email so Outlook will print the line breaks?)

contentEditable поле для захавання новых радкоў пры ўваходзе ў базу дадзеных (contentEditable field to maintain newlines upon database entry)

換行符 (newline character(s))

grep 如何匹配一些字母或行尾 (grep how to match some letters OR end of line)

Trong Python, có thể thoát các ký tự dòng mới khi in một chuỗi không? (In Python, is it possible to escape newline characters when printing a string?)

在 SWIFT 中遇到逗號時將字符串換行 (Break string to newline when meeting a comma in SWIFT)

行尾的'^ M'字符 ('^M' character at end of lines)

從C#中的字符串末尾刪除回車符和換行符 (Removing carriage return and new-line from the end of a string in c#)

新線路和瀏覽器/操作系統兼容性 (New lines and browser/OS compatability)

如何使用 grep 查找單詞後兩個字符之間的所有內容,而不輸出整行? (How do I find everything between two characters after a word using grep, without outputting the entire line?)

在 Perl 中寫入文件的問題 (Issues writing to a file in Perl)

在 for 循環中每 7 行換行一次 (Newline every 7 lines within a for loop)







留言討論