C# 从文件中读取原始字符串
我有以下原始字符串文字: @"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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你为什么不逃避它呢?
why dont you escape it ?
没有与
@
说明符等效的编程方式。说明符仅在编译时使用,将逐字字符串文字转换为其内部表示形式。 (顺便说一句,正确的名称是逐字字符串,而不是原始字符串)。因此,下面的
str1
和str2
在运行时表示完全相同相同的字符串:它们之间唯一的区别仅在源代码中可见。从文本文件读取字符串时,不需要应用任何类型的编程转换。
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
andstr2
below represent exactly the same string at runtime: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.
我认为当你说原始字符串时,你指的是文字字符串。
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.