Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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中替换为空#_C#_Asp.net_String - Fatal编程技术网

C# 字符串双引号在c中替换为空#

C# 字符串双引号在c中替换为空#,c#,asp.net,string,C#,Asp.net,String,我有绳子 There are no items to show in this view of the "Personal Documents" 然后分配给字符串str变量 string str ="There are no items to show in this view of the \"Personal Documents\" library" 现在计划替换“\”并使其成为str对象的实际字符串。我在下面试过,但没有成功 str = str.Replace(@"\",string

我有绳子

There are no items to show in this view of the "Personal Documents"
然后分配给字符串str变量

string str ="There are no items to show in this view 
of the \"Personal Documents\" library"
现在计划替换“\”并使其成为str对象的实际字符串。我在下面试过,但没有成功

str = str.Replace(@"\",string.Empty);
我想str的值应该是

string str ="There are no items to show in this view 
of the "Personal Documents" library"
我需要在另一个字符串中找到这个字符串。在搜索该字符串时。我找不到,因为str包含“\”

这个很好用

启动一个控制台应用程序,用
控制台将str写入控制台。写入
作为信心提升。

表示字符串

There are no items to show in this view of the "Personal Documents" There are no items to show in this view of the "Personal Documents"
  • 和逐字字符串文本

    string str = "There are no items to show in this view of the \"Personal Documents\"";
                                                                 ↑                   ↑
    
    string str = @"There are no items to show in this view of the ""Personal Documents""";
                 ↑                                                ↑                   ↑
    
  • 请注意,在这两种情况下,
    字符都被转义

    在这两种情况下,
    str
    变量包含相同的字符串。例如

    Console.WriteLine(str);
    
    印刷品

    There are no items to show in this view of the "Personal Documents"
    “\”“
    表示由单个字符组成的字符串
    (同样,该字符在此常规字符串文字中转义),而
    是空字符串。

    请尝试:

    string str = @"There are no items to show in this view of the ""Personal Documents"" library"
    

    如果我正确理解了您的问题,您希望将字符串常量的
    \“
    替换为
    。这是不需要的,由编译器为您完成

    之前必须放置
    \
    (转义序列)的原因是告诉编译器您希望在字符串中包含
    ,而不是终止字符串常量

    当存储在内存中时,转义字符已被删除,当使用字符串时(例如,打印在屏幕上),它不会显示

    例如,线路:

    Console.WriteLine("A string with a \" and a \\\" too.");
    
    将打印为:

    A string with a " and a \" too.
    

    您知道“\`字符实际上不是字符串的一部分,而只是用来处理引号的转义字符吗?上面的代码不正确。编译器将返回一个错误。即使使用逐字字符串文字,引号字符仍然需要用附加引号转义(例如,在引用的字符串前面加上@字符)。我注意到了这一点,并在发布答案后立即将其修复。
    string str = @"There are no items to show in this view of the ""Personal Documents"" library"
    
    Console.WriteLine("A string with a \" and a \\\" too.");
    
    A string with a " and a \" too.