Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将\用作转义字符会产生意外的输出_C#_Asp.net_String - Fatal编程技术网

C# 将\用作转义字符会产生意外的输出

C# 将\用作转义字符会产生意外的输出,c#,asp.net,string,C#,Asp.net,String,string sample=“如何添加双引号”; 字符串str='“'+样本+'” 这是我得到的输出-“如何添加双引号” 我希望输出为str=“如何添加双引号” 我使用的是vs code19如果在文本中使用特殊字符或换行符,则可以使用逐字字符串。它们通过在字符串前加一个@来表示。在逐字记录字符串中,您不再需要转义特殊字符,只有一个例外:双引号“需要使用另一个双引号转义: string sample = @""How to add double quotes"&quo

string sample=“如何添加双引号”; 字符串str='“'+样本+'”

这是我得到的输出-
“如何添加双引号”
我希望输出为
str=“如何添加双引号”

我使用的是vs code19

如果在文本中使用特殊字符或换行符,则可以使用逐字字符串。它们通过在字符串前加一个@来表示。在逐字记录字符串中,您不再需要转义特殊字符,只有一个例外:双引号“需要使用另一个双引号转义:

string sample = @""How to add double quotes"";
将逐字字符串与插值字符串组合时,应将$放在第一位:

string sample = $@"He said: ""Yes, for sure!"", but was only {p.Value} ÷ sure about it.";

调试器中显示的输出不是您将在屏幕上收到的输出。
当将其打印到[Console/API/Website]时,一切都将按预期进行

要了解我的意思,请尝试在Main上使用以下代码创建控制台应用程序:

static void Main(string[] args)
{
    string str = "\"This is with doublequotes\"";
    Console.WriteLine(str);
}  

顺便说一句:您只需编写
string str=“\”如何添加双引号\“
,它仍然可以工作。

使用此命令可以解决错误-

string query = "select * from customer where country = '" + USERNAME + "'";

感谢大家投入宝贵的时间!

我甚至尝试了\和@,但得到了相同的输出当你说“输出”时,你是什么意思?你是否运行了
Console.WriteLine(str)
?我在调试器输出中放入了一个调试器,它显示了这个try do str=str.Replace(“\”,string.empty)您将得到相同的结果-您看到的字符串只是在调试器中。我必须在查询字符串中使用它,例如类似这样的内容-从tbl中选择*其中Username=Username假设Username=“abc”的值现在,在查询字符串中,它是-Select*from tbl where username=abc,它应该是Select*from tbl where username=“abc”我希望你明白我的意思@SiddharthKumarShukla同样适用于该查询。如果
string username=“abc”
从tbl中选择*,其中username=\\“”+username+“\”
@管理员不鼓励SQL注入不要这样做。请阅读副本。