我有字符串.
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包含"\".
表达字符串
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("\"", "");
"\""
表示由单个字符组成的字符串"
(同样,在此常规字符串文字中转义),并且""
是空字符串.