C# - 多行逐字字符串不包含Visual Studion中新线条2022中的新线的托架返回(\ r)

发布于 2025-01-17 14:50:04 字数 373 浏览 0 评论 0原文

在 Visual Studio 2022 中定义逐字多行字符串 (@"some string") 时,我们遇到问题。尽管我们在 Windows 计算机上编译代码,并且变量 Environment.NewLine 定义 < code>\r\n 作为新行,逐字多行字符串仅包含 \n。见下文。

MissingCariageReturn

有人知道为什么字符串中缺少回车符吗?

We are facing issue when defining verbatim multiline strings (@"some string") in Visual Studio 2022. Although we are compling code on Windows machine and variable Environment.NewLine defines \r\n as new line, verbatim multiline string contains just \n. See below.

MissingCariageReturn

Does anybody have idea why carriage return is missing in string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2025-01-24 14:50:04

终于找到答案了,感谢 John Skeet 给了我提示。

行为是由代码编辑器引起的 - 在我们的例子中是 Visual Studio 2022。基本上每个代码编辑器都允许您定义换行行为,并且您可以通过 .editorconfig 中的设置覆盖它 - 属性 end_of_line< /code> - 请参阅文档

这是 .editorConfig 的示例文件:

# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
[*.cs]
end_of_line = crlf

您还可以使用 .gitattributes 定义 Git 存储库中文件的行为 文件:

*.cs text eol=crlf

Visual Studio 还允许您使用选项 Edit->Advanced->Set End of Line 重新格式化当前文件序列 但这仅适用于当前打开的文件。
输入图片此处描述

Finally found answer, thanks to John Skeet who gave me a hint.

Behaviour was caused by the code editor - in our case Visual Studio 2022. Basically each code editor allows you to define line breaks behaviour and you can override it by settings in .editorconfig - property end_of_line - see documentation

Here is an example of the .editorConfig file:

# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
[*.cs]
end_of_line = crlf

You can also define behaviour for files in Git repository using .gitattributes file:

*.cs text eol=crlf

Visual Studio also allows you to reformat current file using option Edit->Advanced->Set End of Line Sequence but this is just applied to currently opened file.
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文