Strip double quotes from a string in .NET

后端 未结 12 2028
抹茶落季
抹茶落季 2020-12-24 04:16

I\'m trying to match on some inconsistently formatted HTML and need to strip out some double quotes.

Current:




        
相关标签:
12条回答
  • You can use either of these:

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

    ...but I do get curious as to why you would want to do that? I thought it was good practice to keep attribute values quoted?

    0 讨论(0)
  • 2020-12-24 04:37

    c#: "\"", thus s.Replace("\"", "")

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

    0 讨论(0)
  • 2020-12-24 04:37

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

    0 讨论(0)
  • 2020-12-24 04:37

    This worked for me

    //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;
    
    0 讨论(0)
  • 2020-12-24 04:37
    s = s.Replace( """", "" )
    

    Two quotes next to each other will function as the intended " character when inside a string.

    0 讨论(0)
  • 2020-12-24 04:39

    You have to escape the double quote with a backslash.

    s = s.Replace("\"","");
    
    0 讨论(0)
提交回复
热议问题