Home > front end >  Removing escaped non printable characters from JsonString
Removing escaped non printable characters from JsonString

Time:09-02

I'm getting JsonString from client API and there is some non-printable characters(escaped) in that jsonString:

"{\"Name\":\"\\u001f\\u001f!#$%&'%\\u0001\"}"

I need to remove all this \u001f, \u0001 strings from my string

Regex.Replace("{\"Name\":\"\\u001f\\u001f!#$%&'%\\u0001\"}", @"[^\u0020-\u007E]", string.Empty)

I tried all regexes and all sanitize functions, nothing works, nothing can catch those strings...

Maybe I'm doing something wrong and you can help me.

CodePudding user response:

Let's match every \\udddd and analyze the dddd code:

string text = "{\"Name\":\"\\u001f\\u001f!#$%&'%\\u0001\"}";

// {"Name":"!#$%&'%"}
var result = Regex.Replace(text,
  @"\\u(?<value>[0-9A-Fa-f]{4})",
   m => char.IsControl((char) int.Parse(m.Groups["value"].Value, NumberStyles.HexNumber)) 
     ? "" 
     : m.Value);
  • Related