Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make "%n" equal to "\n"

Tags:

java

string

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.

like image 283
principal-ideal-domain Avatar asked May 12 '15 19:05

principal-ideal-domain


People also ask

Is \n and %n the same Java?

%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.

What is %s \n in C?

%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.

Can we give \n in scanf?

%[^\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.

What does \n do in printf C?

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.


1 Answers

Unfortunately, the only way here is reflection:

Field lineSeparator = System.class.getDeclaredField("lineSeparator");

lineSeparator.setAccessible(true);
lineSeparator.set(null, "\n");
like image 102
Dmitry Ginzburg Avatar answered Oct 01 '22 04:10

Dmitry Ginzburg