Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi.
When I use this code, that's okay:
C#
string str = "Hello\nWorld!";
Console.Write(str);


But, It's not working by using:
C#
Console.Write(Console.ReadLine());


I type:
HTML
Hello\nWorld!

The result is:
HTML
Hello\nWorld!


This code was same:
C#
string temp = Console.ReadLine();
Console.Write(temp);
Console.Write(temp.ToString());


Is there any way to fix it?
Thanks!
Posted
Comments
PIEBALDconsult 7-Dec-14 23:35pm    
Uh, yeah \n only means "newline" when used in a string literal in the C# code, not at the command line.
As an experiment (I don't know whether or not it will work), try typing Ctrl-J, it'll look like ^J. Let us know whether or not it works.

1 solution

Apparently, you don't see the difference between string literal and actual content of a string. Your "\n" is just a string with two characters, '\' and 'n', no more. It does not mean "new line" character (#10). It becomes this character only if your put it in C# string literal; and then it is compiled and turned into an immediate string value in the loaded code. You could understand it in first place, if you paid a bit more attention when your read C# manual or reference:
http://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx[^].

You cannot enter "new line" character in a System.Console read calls, because… this is the end of line. Normally, it is entered in UI with input text box control, where it is done by pressing enter (which, in case of console, simply ends the input). By the way, end of line characters depends on the platform; on Windows, it is usually used with "\r". For further detail, please see: http://en.wikipedia.org/wiki/End_of_line[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900