I'm trying to write a string \n in a text file. But the result in the text file is a newline. I know that the \n represents a newline. But in my case, I really need to see the String \n, not the newline. How can I solve this?
Asked
Active
Viewed 1.9k times
5
-
11have you tried to escape the escape character? example. .\\n ? :) or put it inside quotes '\n'. . – sailhenz Aug 15 '11 at 09:45
-
@DFE: You can vote up comments if you think they help you. Once you've gathered some answers, you should accept one. This will show that you're statistically grateful and ppl are more likely to provide good help :) If you don't, ppl will see that, and are more unwilling to help you next time. – Sebastian Mach Aug 15 '11 at 10:00
6 Answers
18
The \ character escapes the next character, as you say \n will create a newline. If you wish to output an actual \, you need to write:
"\\n"
That way the first slash escapes the second slash, generating an actual slash rather than escaping the n.

Qwerky
- 18,217
- 6
- 44
- 80

Charles Keepax
- 2,392
- 1
- 18
- 19
3
Use "\\n"
. The first backslash escapes the second one and as a result one is printed to your output.

Nicola Musatti
- 17,834
- 2
- 46
- 55
1
Do you mean
pw.print("\\n"); // print \ and n
instead of
pw.print("\n"); // print new line.

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
1
What you want is to print two characters, namely \
and n
. According to your language's manual, to print \
you must escape it. n
is not among the characters you must escape. Therefore, you write \\
and n
, thus \\n
.

Sebastian Mach
- 38,570
- 8
- 95
- 130