当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《字符串双引号在c#中替换为空》经验,为你挑选了1个好方法。

我有字符串.

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包含"\".



1> dtb..:

表达字符串

There are no items to show in this view of the "Personal Documents"

在C#中,您需要转义该"字符,因为"在C#中使用它来包含字符串文字.

有两种选择:

常规字符串文字

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"

另请参见:字符串(MSDN)


编辑:如果你有字符串

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.Replace方法:

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

string result = str.Replace("\"", "");

"\""表示由单个字符组成的字符串"(同样,在此常规字符串文字中转义),并且""是空字符串.

推荐阅读
路人甲
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有