How do I get a platform-dependent new line character?
In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting methods) you can use %n
as in
Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY%n", c);
//Note `%n` at end of line ^^
String s2 = String.format("Use %%n as a platform independent newline.%n");
// %% becomes % ^^
// and `%n` becomes newline ^^
See the Java 1.8 API for Formatter for more details.
C++ - Cross-platform newline character in string
As long as you read/write text streams, or files in text mode, \n
will be translated into the correct sequence for the platform.
http://en.cppreference.com/w/c/io
Getting the platform specific newline character in JavaScript?
Here's what I ended up using:
GetNewLine: function()
{
var OS = Components.classes["@mozilla.org/xre/app-info;1"].
getService(Components.interfaces.nsIXULRuntime).OS;
return /winnt|os2/i.test(OS) ? "\r\n" : /mac/i.test(OS) ? "\r" : "\n";
}
I'm pretty sure the "mac" case should never happen, seeing as it's not listed as a possibility in the OS TARGET variable (which I'm testing via the OS
property in nsIXULRuntime
).
Node.JS constant for platform-specific new line?
Not sure if this is new in the 0.8.x but there is now a constant http://nodejs.org/api/os.html#os_os_eol
const {EOL} = require('os');
How to get current platform end of line character sequence in Rust?
I don't believe there is any feature that does this specifically. Even the line-aware features of the standard library don't: BufRead::read_line
is documented to only recognize \n
, and BufRead::lines
(source), which strips end-of-line characters, only does so for \n
and \r\n
, indiscriminate of what platform it's invoked on.
"Platform line ending" is really a category error, though. Files are sent across networks and copied from one computer to another. If your program writes files that need to be opened on Windows in Notepad, it doesn't matter whether the program that generates them is running on Windows or Linux; it needs to emit \r\n
. Similarly if the program is writing a specific file format or implementing some network protocol; the format or protocol should tell you what line separator to use. If the format allows either and there is no convention, pick the one you prefer; just use it consistently.
If you're reading line endings, you should probably tolerate either one, like BufRead::lines
does.
However, should you really need to, like if your output will be read by a poorly written program that expects different line endings on different platforms, you can use conditional compilation attributes to achieve this effect:
#[cfg(windows)]
const LINE_ENDING: &'static str = "\r\n";
#[cfg(not(windows))]
const LINE_ENDING: &'static str = "\n";
Related Topics
How to Force Garbage Collection in Java
Quality of Image After Resize Very Low - Java
How to Implement a Single Instance Java Application
Spring - @Transactional - What Happens in Background
How to Find the User'S Home Directory in Java
What Causes Javac to Issue the "Uses Unchecked or Unsafe Operations" Warning
How to Lock a File Using Java (If Possible)
Difference Between Hashmap, Linkedhashmap and Treemap
Adding External Library to Artifact Jar in Intellij Idea
Java 8 List≪V≫ into Map≪K, V≫
Getting Hold of the Outer Class Object from the Inner Class Object