简体   繁体   English

对C#字符的特殊含义感到困惑

[英]Confused about C# character special meanings

I'm a newbie in C# programming, and I don't understand how character escaping works. 我是C#编程的新手,我不了解字符转义的工作原理。 I tried many of them, but only '\\t' works like it should. 我尝试了很多,但是只有'\\ t'可以正常工作。 An example for my code: 我的代码的示例:

        textBox1.Text = "asd";
        textBox1.Text = textBox1.Text + '\n';
        textBox1.Text = textBox1.Text + "asd";

MultiLine is enabled, but my output is simply "asdasd" without breaking the line. 启用了MultiLine,但我的输出只是“ asdasd”而没有中断。 I hope some of you knows what the answer is. 我希望你们中的一些人知道答案是什么。

You need "\\r\\n" for a line break in Windows controls, because that's the normal line break combination for Windows. Windows控件中的换行符需要"\\r\\n" ,因为这是Windows的常规换行符组合。 (Line breaks are the bane of programmers everywhere. Different operating systems have different character sequences that they use as their "typical" line breaks.) (换行符是程序员们的祸根。不同的操作系统使用不同的字符序列作为“典型”换行符。)

"\\r\\n" is two characters: \\r for carriage return (U+000D) and \\n for line feed (U+000A). "\\r\\n"是两个字符: \\r用于回车(U + 000D)和\\n用于换行(U + 000A)。 You don't need to do it in three statements though: 不过,您不需要在以下三个语句中这样做:

textBox1.Text = "First line\r\nSecond line";

Now, I've deliberately gone with \\r\\n there instead of Environment.NewLine , on the grounds that if you're working with System.Windows.Forms , those will be Windows-oriented controls. 现在,我故意在其中使用\\r\\n而不是Environment.NewLine ,因为如果使用System.Windows.Forms ,则这些控件将是面向Windows的控件。 It's unclear to me what an implementation of Windows Forms will do on Mac or Linux, where the normal line break is different. 我不清楚在正常的换行符不同的Mac或Linux上Windows Forms的实现将如何执行。 (My guess is that a pragmatic implementation will break on any of \\r , \\n or \\r\\n , just like TextReader does, for simplicity.) (为简单起见,我的猜测是实用的实现会破坏\\r\\n\\r\\n ,就像TextReader一样。)

Sometimes - such as if you're writing a text file for consumption on the same machine - it's good to use Environment.NewLine . 有时-例如,如果您要在同一台计算机上编写文本文件以供使用-最好使用Environment.NewLine

Sometimes - such as when you're implementing a network protocol such as HTTP which mandates a specific line break format - it's good to be explicit about it. 有时-例如当您实现要求特定的换行格式的HTTP之类的网络协议时-最好对其进行明确说明。

Sometimes - such as in this case - it's just not clear. 有时-例如在这种情况下-尚不清楚。 However, it's always worth thinking about. 但是,始终值得考虑。

For a complete list of escape sequences in C#, you can either look at the C# language specification (always fun!) or look at my Strings article which contains that information and more. 有关C#中转义序列的完整列表,您可以查看C#语言规范(总是很有趣!),也可以查看我的Strings文章 ,其中包含该信息以及更多内容。

The most legible way to insert a newline in C# is: 在C#中插入换行符的最清晰的方法是:

    textBox1.Text = "asd";
    textBox1.Text = textBox1.Text + Environment.NewLine;
    textBox1.Text = textBox1.Text + "asd";

This code snippet would set textBox1 text to " asd ", then add a newline to it, then on the second line, it would add " asd " again. 此代码段会将textBox1文本设置为“ asd ”,然后向其中添加换行符,然后在第二行中再次添加“ asd ”。

The special characters you are confused about are artifacts from when computers used teletype printers for all of their output. 您感到困惑的特殊字符是计算机将电传打印机用于所有输出时的伪影。 Teletype printers predated digital displays by several years. 电传打字机比数字显示器要早几年。

  • \\n tells the printer (output) to move the print head down one row (new line). \\ n告诉打印机(输出)将打印头向下移动一行(新行)。
  • \\r tells the printer (output) to move the print head back to the start of the row (carriage return). \\ r告诉打印机(输出)将打印头移回到行的开头(回车)。

Combined, this resulted in the print head being positioned at the start of the next row, ready for output onto clean paper. 综合起来,这导致打印头位于下一行的开始位置,准备输出到干净的纸上。

Further information can be sought out on the internet with ease, including places such as Wikipedia's NewLine article 可以在Internet上轻松找到更多信息,例如Wikipedia的NewLine文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM