Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# replace not changing the input string

I have a JSON string like {"age":"N\/A"}, now I want to replace this N/A with some static text say empty so the JSON string is converted to {"age":empty}

This is my code:

string updated = Regex.Replace(jsonstring, "(\"N\\/A\")", "empty");

Console.WriteLine(updated);

After running the code I am seeing the updated string is not modified. it is still {"age":"N\/A"}. How to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to escape characters in your regex as well, your code should look like this:

string jsonstring = "{\"age\":\"N\\/A\"}";
string updated = Regex.Replace(jsonstring, "(\"N\\\\/A\")", "empty");
Console.WriteLine(updated);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading