C# How to handle double quotes in string replace

Hi

How do I replace '\"' with '"' globally in C#
Backslash  Double Quotes replace with Double Quotes
and remove Double Quotes from the end of a string

string MyString = "Lorem \"ipsum\" dolor sit amet \"consectetuer\" ultrices turpis Phasellus et odio. Urna."";
MyString.Replace('\\"', '"'); // Error CS1012  C# Too many characters in character literal
MyString.Replace("\\"", "\""); didn't work

Console.WriteLine(MyString);
// Expected output
Lorem "ipsum" dolor sit amet "consectetuer" ultrices turpis Phasellus et odio. Urna.

Open in new window

LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
While I see some inconsistencies in your code, I assume you want this:

fix string:
string MyString = "Lorem \"ipsum\" dolor sit amet \"consectetuer\" ultrices turpis Phasellus et odio. Urna.";

Open in new window


add on top:
using System.Text.RegularExpressions;

Open in new window


then use:
MyString = Regex.Replace(MyString, @"[\""]", "", RegexOptions.None); 

Open in new window


Because your expected output was already the original string. Now the output is without any quotes.
As I said, due to inconsistencies, I had to figure out what you want on my own. If that's not what you want, state it more clearly.

EDIT:
Thinking longer about it now, I think you want broken code to be fixed magically. That's not possible. If the code is already wrong on top, it won't compile, and the rest of the code is moot (including your paradoxal fixing code)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
OK. Your opening line won't compile so that's wrong right from the start.

Secondly, if you Console.WriteLine() your string without doing any string replacement, you will get the correct results. The backslash simply escapes the quotes, so it won't be output.

Calling Replace on your string will currently do nothing as you're not assigning the return value to anything.

If you absolutely want to replace a \" with a ", then you need to escape both the slash and the quote. The escape character also happens to be the slash, so what you end up with is:

var newText = MyString.Replace("\\\"", "\"");

Maybe explain a little more about what you're trying to do and where your original text is coming from and we may be able to offer some more suitable advice.
trevor1940Author Commented:
Hi
Thank you the regex was what I needed

What I'm trying to do is clean up up some badly formed  HTML so I can parse it using HtmlAgilityPack;

Replacing the
\"
was an obvious quick fix

The code I posted was only to try and illustrate the point
trevor1940Author Commented:
I used the regex  but can Chris solution also working
FYI
For some reason the whole HTML file / string was in Double Quote marks so I had to remove these from the first and last characters then remove the internal \ backslashes
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.