C# 从.NET中的字符串中删除双引号

C# 从.NET中的字符串中删除双引号,c#,.net,vb.net,C#,.net,Vb.net,我试图匹配一些格式不一致的HTML,需要去掉一些双引号 当前: <input type="hidden"> 用空字符串替换双引号的语法/转义字符组合是什么 s = s.Replace("\"", ""); s = s.Replace("\"", ""); 需要使用\来转义字符串中的双引号字符。必须使用反斜杠转义双引号 s = s.Replace("\"",""); 我认为您的第一行实际上可以工作,但我认为对于包含一个引号的字符串,您需要四个引号(至少在VB中): 对于C#,您

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

当前:

<input type="hidden">
用空字符串替换双引号的语法/转义字符组合是什么

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

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

必须使用反斜杠转义双引号

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

我认为您的第一行实际上可以工作,但我认为对于包含一个引号的字符串,您需要四个引号(至少在VB中):

对于C#,您必须使用反斜杠将引号转义:


您可以使用以下任一选项:

s = s.Replace(@"""","");
s = s.Replace("\"","");
…但我很好奇你为什么要这么做?我认为将属性值保留为引号是一种好的做法?

c:
“\”
,因此
s.Replace(“\”,”)

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

相邻的两个引号将在字符串中充当预期的“字符”。

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;

我没有看到我的想法已经被重复,因此我建议您查看Microsoft C文档中的
string.Trim
,您可以添加要修剪的字符,而不是简单地修剪空白:

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

如果您只想从字符串的末尾(而不是中间)去掉引号,并且字符串的任意一端都可能有空格(即,解析逗号后有空格的CSV格式文件),则不应将引号改为“hello”,而不是“hello”,则需要调用修剪函数两次…例如:

string myStr = " \"sometext\"";     //(notice the leading space)
myStr = myStr.Trim('"');            //(would leave the first quote: "sometext)
myStr = myStr.Trim().Trim('"');     //(would get what you want: sometext)

如果您想删除单个字符,我想简单地读取数组并跳过该字符并返回数组会更容易。我在自定义解析vcard的json时使用它。 因为它是带有“引号”文本标识符的糟糕json

将下面的方法添加到包含扩展方法的类中

  public static string Remove(this string text, char character)
  {
      var sb = new StringBuilder();
      foreach (char c in text)
      {
         if (c != character)
             sb.Append(c);
      }
      return sb.ToString();
  }
然后可以使用此扩展方法:

var text= myString.Remove('"');

我正在使用HTML Agility Pack查找某个链接,然后我需要从HTML文本中删除该链接中的一个值。HTML Agility Pack引用属性值,但未引用原始HTML。(所有这些都用于一个测试。)如果字符串中有更多的嵌入引号,则会产生副作用如果字符串中有更多的嵌入引号,则会产生副作用如果字符串中有更多的嵌入引号,则会产生副作用如果字符串中有更多的嵌入引号,则会产生副作用如果字符串中有更多的嵌入引号,则会产生副作用字符串中的TE如果字符串中有更多的嵌入引号,则会产生副作用如果字符串中有更多的嵌入引号,则会产生副作用如果字符串中有更多的嵌入引号,则会产生副作用
//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;
string withQuotes = "\"hellow\"";
string withOutQotes = withQuotes.Trim('"');
string myStr = " \"sometext\"";     //(notice the leading space)
myStr = myStr.Trim('"');            //(would leave the first quote: "sometext)
myStr = myStr.Trim().Trim('"');     //(would get what you want: sometext)
  public static string Remove(this string text, char character)
  {
      var sb = new StringBuilder();
      foreach (char c in text)
      {
         if (c != character)
             sb.Append(c);
      }
      return sb.ToString();
  }
var text= myString.Remove('"');