Strip double quotes from a string in .NET

后端 未结 12 2204
抹茶落季
抹茶落季 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条回答
  •  轻奢々
    轻奢々 (楼主)
    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;
    

提交回复
热议问题