C# 中的回车

Saad Aslam 2023年10月12日
  1. 使用无参数 Console.WriteLine() 在 C# 中添加新行
  2. 在 C# 中的同一字符串中注入新行
  3. 在 C# 中使用 Environment.NewLine
  4. 在 C# 中使用换行符的 ASCII 文字
  5. 使用\r\n 在 C# 中插入新行的字符
  6. 在 ASP.NET 中为 C# 中的现有字符串插入新行
C# 中的回车

本文将解释如何使用 C# 编程语言在字符串中生成回车符。在 C# 字符串中创建回车可能以不同的方式完成。

以下技术,它们都以类似的方式在 C# 字符串中构造回车,被检查和实现。

使用无参数 Console.WriteLine() 在 C# 中添加新行

使用无参数 console.WriteLine() 是向控制台程序添加新行的最简单方法。例如:

Console.WriteLine("This is a line");
Console.WriteLine();
Console.WriteLine("This is the second line");

输出:

This is a line

This is the second line

使用没有任何参数的 Console.WriteLine() 方法会导致行终止符,如上面 Microsoft 文档中所示。为方便起见,WriteLine() 函数在每组两行之间插入一个换行符。

除了默认间距之外,你还可以更改此设置以满足你的需要。例如,可以重写 Console.WriteLine() 方法以打印两行而不是单行。

像下面这样的代码可能会解决这个问题。

Console.Out.NewLine = "\r\n\r\n";
Console.WriteLine("This is the first line");
Console.WriteLine();
Console.WriteLine("This is the second line");

控制台可以使用上面的代码。使用 WriteLine() 插入两行。

因此可以以这种方式传递一个字符串。当你使用 WriteLine() 时,当前输入的字符串将被扩展一行。

另一方面,控制台没有参数。WriteLine() 现在将输入两行,而不是仅一行。

使用此代码,你会得到类似的结果。

This is the first line


This is the second line

请注意,在第一个控制台之后,还有另一行。第二个控制台使用 WriteLine(),然后是两个空行。

第三次调用 WriteLine() 后有一个空行。

在 C# 中的同一字符串中注入新行

\n 放在字符串中的任何位置是向现有字符串添加新行的第二个选项。例如:

Console.WriteLine("This is a line.\nThis is the second line.");

上面的代码将如下所示运行。

This is a line.
This is the second line.

如前所述,\n 可以成功地在同一字符串中插入新行。

单个反斜杠使用转义序列和字符执行特定操作或功能。当插入到代码字符串文字中时,转义序列表示除双引号之外的所有内容。

转义序列可以被翻译。字符串错误会导致转义序列问题。 \t 制表符,\a 哔哔声。

在 C# 中使用 Environment.NewLine

\r\n 是 Windows 等效的换行符。有几种方法可以在 C# 字符串中插入换行符,最佳方法取决于你的软件。

如果你想在一个字符串中开始一个新行,你可以使用 Environment.NewLine。例如:

Console.WriteLine("This is a line." + Environment.NewLine + "This is the second line.");

输出:

This is a line.
This is the second line.

上面的代码将产生与前面的结果相同的结果。

新行、换行、换行、回车、CRLF 和行尾与 NewLine 的功能一起使用时表示相同的含义。换行符,例如转义字符 \r\nvbCrLf(在 Visual Basic 中)可以与 NewLine 结合使用,并结合特定语言的换行符支持 Microsoft C# 和 C/C++。

控制台会在它处理的任何文本之后自动插入一个新行。WriteLineStringBuilder 类方法用于添加行。

在 C# 中使用换行符的 ASCII 文字

这种方法类似于\n。另一方面,你可以改用表示新行的文字 ASCII 字符。

例如:

Console.WriteLine("This is a line. \x0AThis is the second line.");

输出:

This is a line.
This is the second line.

这个用于换行的 ASCII 文字 x0A 同样会在上面看到的文本中插入一个新行。

使用\r\n 在 C# 中插入新行的字符

你可以将 \r\n 注入字符串以引入新行。例如:

Console.WriteLine("This is a line. \r\nThis is the second line.");

输出:

This is a line.
This is the second line.

上面的代码使用\r\n 正确地插入了一个新行。 \r\n\n 这两个术语之间是否存在显着差异?

\n 代表换行,\r 代表回车,将光标移动到文档的最左侧。要从左侧开始打印,需要使用 \n 进行换行,使用 \r 进行回车到最左边的位置;这是来自保存在 Windows 平台上的古代计算机语法。

  1. \r\n 是 Windows 风格
  2. \n 是一种 POSIX 风格
  3. \r 是旧的 pre-OS X Macs 风格,现代 Mac 使用 POSIX 风格

在 ASP.NET 中为 C# 中的现有字符串插入新行

HTML 或 ASP.NET 中的换行符由 <br/> 标记表示。通常需要将包含换行符的现有字符串转换为包含 <br/> 标记的 HTML 代码。

你可以使用正则表达式通过以下方法将字符串的新行转换为 HTML 换行符。

public static string ConvertToLineBreaks(this string inputString) {
  Regex regEx = new Regex(@"(\\n|\\r)+");
  return regEx.Replace(inputString, "");
}

上面显示的方法旨在用作扩展方法。将该方法包含在一个单独的文件中,然后使用 using 语句将其包含在你的代码中。

最后,调用方法如下。

string html = inputString.ConvertToLineBreaks();

你可以在此处测试上述正则表达式。

作者: Saad Aslam
Saad Aslam avatar Saad Aslam avatar

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

LinkedIn

相关文章 - Csharp String