如何从字符串中消除转义符

本文关键字:转义 字符串 | 更新日期: 2023-09-27 18:26:57

我有一个Json以字符串形式保存在数据库中:

"{ '"path'" : '"a.crx'", '"uniteDeVolume'" : '"g or ml'", '"unites'" :     '"Acts/Volume'", '"nbIndevMin'" : '"20'", '"nbJours'" : '"6 to 7'", '"ventilDepart'" : '"20051'" }"

当我想从数据库中读取这个字段,并将其反序列化为BsonDocument时,返回的值是

"'"{ '''"path'''" : '''"a.crx'''", '''"uniteDeVolume'''" : '''"g or ml'''", '''"unites'''" : '''"Acts/Volume'''", '''"nbIndevMin'''" : '''"20'''", '''"nbJours'''" : '''"6 to 7'''", '''"ventilDepart'''" : '''"20051'''" }'""

如何命名转义字符?

如何从字符串中消除转义符

我会使用来自"System.Text.RegularExpressions"的Regex

var escapedString ="'"{ '''"path'''" : '''"a.crx'''", '''"uniteDeVolume'''" : '''"g or ml'''", '''"unites'''" : '''"Acts/Volume'''", '''"nbIndevMin'''" : '''"20'''", '''"nbJours'''" : '''"6 to 7'''", '''"ventilDepart'''" : '''"20051'''" }'"";
var unescapedString = Regex.Unescape(escapedString);

返回以下

"{"path":"a.crx","uniteDeVolume":"g或ml","unions":"Acts/Volume","nbIndevMin":"20","nbJours":"6到7","ventilDepart":"20051"}"

您可以访问此链接,它可能会帮助您

http://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma