为何选择“\ n”使用Console.Wite(Console.Readline())时无法检测到? [英] Why "\n" cannot be detected when using Console.Wite(Console.Readline())?

查看:130
本文介绍了为何选择“\ n”使用Console.Wite(Console.Readline())时无法检测到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

当我使用这段代码时,没关系:

  string  str =   Hello\\\
nWorld!
;
Console.Write(str);





但是,它无法使用:

 Console.Write(Console.ReadLine()); 





我键入:

 Hello \\\
World!



结果是:

 Hello\\\
World!





此代码相同:

  string  temp = Console.ReadLine(); 
Console.Write(temp);
Console.Write(temp.ToString());





有什么方法可以修复它吗?

谢谢!

解决方案

显然,您没有看到字符串的字符串文字和实际内容之间的区别。你的\ n只是一个包含两个字符的字符串,'\'和'n',不再是。它并不意味着新行字符(#10)。只有当你把它放在C#字符串文字中时才会成为这个字符;然后它被编译并在加载的代码中变成一个立即的字符串值。你可以在第一时间理解它,如果你在阅读C#手册或参考时多付了一点:

http://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx [ ^ ]。



你不能在 System.Console 读取调用中输入换行符字符,因为......这就是结束线。通常,它在输入文本框控件的UI中输入,按Enter键完成(在控制台的情况下,只需输入输入)。顺便说一句,行尾字符取决于平台;在Windows上,它通常与\ r一起使用。有关详细信息,请参阅: http://en.wikipedia.org/wiki/End_of_line [ ^ ]。



< DD> -SA

Hi.
When I use this code, that's okay:

string str = "Hello\nWorld!";
Console.Write(str);



But, It's not working by using:

Console.Write(Console.ReadLine());



I type:

Hello\nWorld!


The result is:

Hello\nWorld!



This code was same:

string temp = Console.ReadLine();
Console.Write(temp);
Console.Write(temp.ToString());



Is there any way to fix it?
Thanks!

解决方案

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


这篇关于为何选择“\ n”使用Console.Wite(Console.Readline())时无法检测到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆