Why isn't b
equal to true
if you run this code on Windows?
System.setProperty("line.separator", "\n");
String s=String.format("%n");
boolean b="\n".equals(s);
I want s
to be "\n"
and not "\r\n"
, even on Windows.
%n is special code (placeholder) for new-line symbol (that might be \r\n or \n) in formatted string and \n is an actual symbol. You can think of %n as a symbol that will be replaced with the \r\n or \n in the resulting string.
%s refers to a string %d refers to an integer %c refers to a character. Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0]. Follow this answer to receive notifications.
%[^\n] It is an edit conversion code. The edit conversion code %[^\n] can be used as an alternative of gets. C supports this format specification with scanf() function.
The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.
Unfortunately, the only way here is reflection:
Field lineSeparator = System.class.getDeclaredField("lineSeparator");
lineSeparator.setAccessible(true);
lineSeparator.set(null, "\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With