For example, there is a line:

String str = "text\ntext"; 

If you withdraw it:

 System.out.printIn(str); 

Here is what I get:

 text text 

How to display a string like this?

 text\ntext 
  • 2
    Add replace('\n','\\n') - nick_n_a
  • System.out.println (a.replace ("\ n", "\\\ n")); Just "and not". - Roman

1 answer 1

I am programming languages ​​and not only after the sign \ are special characters. \n is a newline character. Therefore, stdout shows, following this line character, from a new line. In order to get the result that you need, you need to escape the \ character. You need to replace the source text with text\\ntext , or write System.out.printIn(str.replace('\n','\\n')); before output System.out.printIn(str.replace('\n','\\n')); as @ nick-na wrote