Even Mien
Even Mien

Reputation: 45958

Strip double quotes from a string in .NET

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?

Upvotes: 130

Views: 270248

Answers (12)

Walter Verhoeven
Walter Verhoeven

Reputation: 4431

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('"');

Upvotes: 1

TJC
TJC

Reputation: 111

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)

Upvotes: 7

Shane
Shane

Reputation: 731

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""

Upvotes: 63

user3519062
user3519062

Reputation: 21

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;

Upvotes: 2

Joey
Joey

Reputation: 354854

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("\"", "");

Upvotes: 265

gHeidenreich
gHeidenreich

Reputation: 79

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

Upvotes: 3

Mike
Mike

Reputation: 327

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

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

Upvotes: 1

Svante Svenson
Svante Svenson

Reputation: 12478

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

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

Upvotes: 6

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158389

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?

Upvotes: 19

Steve Gilham
Steve Gilham

Reputation: 11277

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

Upvotes: 9

Jake Pearson
Jake Pearson

Reputation: 27757

You have to escape the double quote with a backslash.

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

Upvotes: 4

David
David

Reputation: 25470

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

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

Upvotes: 31

Related Questions