[英]Reading nested curly brackets as part of string
I have password like this iD&NAAY#{.x}NqpzK|%
. 我有这样的密码
iD&NAAY#{.x}NqpzK|%
。
When I am trying to store this in string variable like this 当我试图将其存储在这样的字符串变量中时
string password = $"\\"iD&NAAY#{.x}NqpzK|%\\""
I am getting a compile time error (invalid expression term) in this part {.x}
我在
{.x}
部分遇到编译时错误(无效的表达式术语)
How can I avoid this and read this password as a full string type? 如何避免这种情况并以完整的字符串类型读取此密码?
Simply don't use an interpolated string , that means, define the string without the $
sign prefix. 只是不要使用插值字符串 ,这意味着定义不带
$
符号前缀的字符串。
When you use the $
sign prefix, C# is parsing your string, trying to find variable references in it and replace them with variable values. 当您使用
$
符号前缀时,C#会解析您的字符串,尝试在其中查找变量引用并将其替换为变量值。 The format for variable references in strings is {variable_name}
. 字符串中变量引用的格式为
{variable_name}
。
var test = "abcd";
var interpolated_string = $"Test: {test}";
The variable interpolated_string
will contain the string Test: abcd
. 变量
interpolated_string
将包含字符串Test: abcd
。
If you remove the $
sign prefix, the string will not be processed and it will be taken as a literal: 如果删除
$
符号前缀,则不会处理该字符串,并将其当作文字:
var test = "abcd";
var interpolated_string = "Test: {test}";
The variable interpolated_string
will contain the string Test: {test}
now. 变量
interpolated_string
将包含字符串Test: {test}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.