Strip double quotes from a string in .NET

C#.Netvb.net

C# Problem Overview


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

Current:

<input type="hidden">

The Goal:

<input type=hidden>

This is wrong because I'm not escaping it properly:

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

This is wrong because there is not blank character character (to my knowledge):

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

What is syntax / escape character combination for replacing double quotes with an empty string?

C# Solutions


Solution 1 - C#

I think your first line would actually work but I think you need four quotation marks for a string containing a single one (in VB at least):

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

for C# you'd have to escape the quotation mark using a backslash:

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

Solution 2 - C#

I didn't see my thoughts repeated already, so I will suggest that you look at string.Trim in the Microsoft documentation for C# you can add a character to be trimmed instead of simply trimming empty spaces:

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

should result in withOutQuotes being "hello" instead of ""hello""

Solution 3 - C#

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

You need to use the \ to escape the double quote character in a string.

Solution 4 - C#

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?

Solution 5 - C#

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

Solution 6 - C#

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

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

Solution 7 - C#

You have to escape the double quote with a backslash.

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

Solution 8 - C#

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

Solution 9 - C#

If you only want to strip the quotes from the ends of the string (not the middle), and there is a chance that there can be spaces at either end of the string (i.e. parsing a CSV format file where there is a space after the commas), then you need to call the Trim function twice...for example:

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)

Solution 10 - C#

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;

Solution 11 - C#

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

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

Solution 12 - C#

if you would like to remove a single character i guess it's easier to simply read the arrays and skip that char and return the array. I use it when custom parsing vcard's json. as it's bad json with "quoted" text identifiers.

Add the below method to a class containing your extension methods.

  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();
  }

you can then use this extension method:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEven MienView Question on Stackoverflow
Solution 1 - C#JoeyView Answer on Stackoverflow
Solution 2 - C#ShaneView Answer on Stackoverflow
Solution 3 - C#DavidView Answer on Stackoverflow
Solution 4 - C#Fredrik MörkView Answer on Stackoverflow
Solution 5 - C#Steve GilhamView Answer on Stackoverflow
Solution 6 - C#Svante SvensonView Answer on Stackoverflow
Solution 7 - C#Jake PearsonView Answer on Stackoverflow
Solution 8 - C#gHeidenreichView Answer on Stackoverflow
Solution 9 - C#TJCView Answer on Stackoverflow
Solution 10 - C#user3519062View Answer on Stackoverflow
Solution 11 - C#MikeView Answer on Stackoverflow
Solution 12 - C#Walter VerhoevenView Answer on Stackoverflow