How can you put escape characters (like newline character -- \n) to a CString?
7 Answers
\n becomes a box (garbage character). it doesn't insert a new line.
Is this what you find in the debugger? If so, this is okay. The newline character has a hex value of 0xA or 10 in decimal. Since this is a non-printable character, that's what the debugger will show you.
Apart from that, if you are using notepad to view the output, it may not come out right. Try "\r\n" in order to get notepad to split correctly.

- 108,024
- 16
- 131
- 187
-
I was adding code to a ROOT project, and had the same problem. The \r\n did the trick! – Tom Sep 26 '11 at 18:40
I think your problem is not inserting a newline in the CString, but in the method that you are using to display the string

- 32,610
- 9
- 70
- 97
When I was a younger coder I had the same problem when writing to a .txt file on windows and opening it in notepad. The newline characters show up as these squares. If you want the appearance of newlines in notepad use "\r". This isn't really a solution, but I have an intuition that this is your problem...

- 4,446
- 4
- 18
- 11
CString cs = "First sentence."+"\015\012"+"Second sentence."
I hope it would help you.

- 351
- 1
- 11
Do you mean an unescaped version? If so then just escape the escape
CString("\\n");

- 733,204
- 149
- 1,241
- 1,454
The newline character is '\n' in single quotes. Just add that to your cstring.

- 5,262
- 1
- 19
- 22
CString strMessage = "";
strMessage.Format("Alpha \n Beta");
"\n" appears as box character in debugger but while displaying on the UI it displays properly.

- 4,743
- 7
- 33
- 43