C# 从文件中读取原始字符串

发布于 2025-01-03 23:52:25 字数 258 浏览 2 评论 0原文

我有以下原始字符串文字: @"some text ""here"" and some more text" 当我将其分配给程序中的字符串变量时,一切正常。
但是当我输入这个字符串 - "some text ""here"" and some more text" 并读取它时,它不会被识别为原始字符串。

我应该怎么做才能让 C# 将其识别为原始字符串?具体来说,用于指示该字符串是原始字符串的“@”说明符的编程等效项是什么?

I have the following raw-string literal : @"some text ""here"" and some more text"
Everything works fine when I have this assigned to a string variable in a program.
But when I put this string - "some text ""here"" and some more text" and read it, it is not being recognized as a raw string.

What should I do to have C# recognize this as a raw-string? Specifically, what is the programmatic equivalent of the '@' specifier used to indicate that the string is a raw-string?

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

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

发布评论

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

评论(3

疯到世界奔溃 2025-01-10 23:52:25

你为什么不逃避它呢?

"some text \"here\" and some more text"

在此处输入图像描述

why dont you escape it ?

"some text \"here\" and some more text"

enter image description here

北音执念 2025-01-10 23:52:25

没有与 @ 说明符等效的编程方式。说明符仅在编译时使用,将逐字字符串文字转换为其内部表示形式。 (顺便说一句,正确的名称是逐字字符串,而不是原始字符串)。

因此,下面的 str1str2 在运行时表示完全相同相同的字符串:

string str1 = "some text \"here\" and some more text";
string str2 = @"some text ""here"" and some more text";

它们之间唯一的区别仅在源代码中可见。从文本文件读取字符串时,不需要应用任何类型的编程转换。

There's no programmatic equivalent of the @ specifier. The specifier is used only at compile time to convert a verbatim string literal to its internal representation. (BTW, the proper name is verbatim string, not raw-string).

Therefore, str1 and str2 below represent exactly the same string at runtime:

string str1 = "some text \"here\" and some more text";
string str2 = @"some text ""here"" and some more text";

The one and only difference between them is visible in your source code only. You don't need to apply any kind of programmatic transformation when your read strings from a text file.

对不⑦ 2025-01-10 23:52:25

我认为当你说原始字符串时,你指的是文字字符串。

http://msdn.microsoft.com/en- us/library/aa691090(v=vs.71).aspx

使用 @ 符号的文字字符串只是告诉编译器按字面意思获取所提供的文本。

据我所知,没有办法将字符串转换为该字符串的文字版本。

但是您可以轻松编写用单引号替换双引号的代码。

I think when you say raw string, you mean literal string.

http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

Literal strings using the @ symbol are just telling the compiler to literally take the text provided.

As far as I know there is no way to convert a string into the literal version of that string.

But you could easily write that code that replaces double quotes with single quotes.

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