从.NET中的string中去除双引号

我试图匹配一些格式不一致的HTML,并需要去掉一些双引号。

当前:

<input type="hidden"> 

目标:

 <input type=hidden> 

这是错误的,因为我没有正确地转义它:

s = s.Replace(“”“,”“);

这是错误的,因为没有空白的字符(据我所知):

 s = s.Replace('"', ''); 

什么是用空stringreplace双引号的语法/转义字符组合?

我认为你的第一行实际上工作,但我认为你需要四个引号包含一个单一的string(至less在VB):

 s = s.Replace("""", "") 

对于C#,您必须使用反斜杠来避免引号:

 s = s.Replace("\"", ""); 
 s = s.Replace("\"", ""); 

您需要使用\来转义string中的双引号字符。

您可以使用以下任一项:

 s = s.Replace(@"""",""); s = s.Replace("\"",""); 

…但是我好奇你为什么要这么做? 我认为这是保持属性值引用的好习惯吗?

我没有看到我的想法已经重复,所以我会build议你看看string.Trim微软的C#文档中,你可以添加一个字符来修剪而不是简单地修剪空格:

 string withQuotes = "\"hellow\""; string withOutQotes = withQuotes.Trim('"'); 

应该导致withOutQuotes是"hello"而不是""hello""

 s = s.Replace("\"",string.Empty); 

您必须用反斜杠来避免双引号。

 s = s.Replace("\"",""); 

c#: "\"" ,因此s.Replace("\"", "")

vb / vbs / vb.net: ""因此s.Replace("""", "")

 s = s.Replace( """", "" ) 

两个相邻的引号将在string内作为预期的“字符”。

s = s.Replace(@“”“”,“”);

这对我有效

 //Sentence has quotes string nameSentence = "Take my name \"Wesley\" out of quotes"; //Get the index before the quotes`enter code here` int begin = nameSentence.LastIndexOf("name") + "name".Length; //Get the index after the quotes int end = nameSentence.LastIndexOf("out"); //Get the part of the string with its quotes string name = nameSentence.Substring(begin, end - begin); //Remove its quotes string newName = name.Replace("\"", ""); //Replace new name (without quotes) within original sentence string updatedNameSentence = nameSentence.Replace(name, newName); //Returns "Take my name Wesley out of quotes" return updatedNameSentence;