How do I get a platform-independent newline in Java? I can’t use "\n"
everywhere.
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.
. \\ .
(less the dots) produces a single backslash and %%
produces a single %
. –
Lymphadenitis %
from user input! –
Lacquer %%n
is supposed to mean. In any case, the formatted text is supposed to read, "Use %n as a platform independent newline."
with a newline character at the end. –
Bigamous s
is a user provided string, you should not use it in the format string, e.g. format(s+"%n")
, because if s contains %
this will fail. It is fine to use user provided strings as format arguments. –
Lacquer %
as the final character. But in that case, the format would be invalid if you used s+"\n"
. (Which would you rather have when the user supplies such a format string--an exception or a missing newline? I think it's a toss-up.) –
Cathead String.format(s + "%n")
is easily solved by String.format("%s%n", s)
. It is always risky to involve user input as format body (in the same way as eval()
). –
Footless Java 7 now has a System.lineSeparator()
method.
lineSeperator(int)
which returns some number of line seperators, as I often find myself using 2 at once. –
Checkerboard String.join("", Collections.nCopies(5, System.lineSeparator()))
–
Gaylagayle System.lineSeparator().repeat(5)
–
Brakpan You can use
System.getProperty("line.separator");
to get the line separator
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.
. \\ .
(less the dots) produces a single backslash and %%
produces a single %
. –
Lymphadenitis %
from user input! –
Lacquer %%n
is supposed to mean. In any case, the formatted text is supposed to read, "Use %n as a platform independent newline."
with a newline character at the end. –
Bigamous s
is a user provided string, you should not use it in the format string, e.g. format(s+"%n")
, because if s contains %
this will fail. It is fine to use user provided strings as format arguments. –
Lacquer %
as the final character. But in that case, the format would be invalid if you used s+"\n"
. (Which would you rather have when the user supplies such a format string--an exception or a missing newline? I think it's a toss-up.) –
Cathead String.format(s + "%n")
is easily solved by String.format("%s%n", s)
. It is always risky to involve user input as format body (in the same way as eval()
). –
Footless If you're trying to write a newline to a file, you could simply use BufferedWriter's newLine() method.
The commons-lang library has a constant field available called SystemUtils.LINE_SEPARATOR
StringBuilder newLine=new StringBuilder();
newLine.append("abc");
newline.append(System.getProperty("line.separator"));
newline.append("def");
String output=newline.toString();
The above snippet will have two strings separated by a new line irrespective of platforms.
Since JDK 1.1, the BufferedWriter class had the "newLine()" method which wrote the platform-dependent new line. It also provided the StringWriter class, making it possible to extract the new line:
public static String getSystemNewLine() {
try {
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
bw.newLine();
bw.flush();
String s = sw.toString();
bw.close();
return s;
} catch (Exception e) {
throw new Error(e);
}
}
Avoid appending strings using String + String etc, use StringBuilder instead.
String separator = System.getProperty( "line.separator" );
StringBuilder lines = new StringBuilder( line1 );
lines.append( separator );
lines.append( line2 );
lines.append( separator );
String result = lines.toString( );
string + string
". –
Protrusion © 2022 - 2024 — McMap. All rights reserved.