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

how to remove all escaped characters (that are not special characters)

I am parsing some WMI query results, and some values that are empty in Powershell’s Get-CimInstance get the value of "\0" from C#’s ManagementObjectSearcher.

I’m trying to filter them out and replace with null/empty string, but I encountered something I don’t fully understand:

s = "\0"; // assigned by ManagementObjectSearcher, value displayed by VS 2019 debugger
var t1 = s == "\0";  // true
var t2 = s == @"\0"; // false
var t3 = string.IsNullOrWhiteSpace(s); // false

The value s is displayed as "\0" in debugger, but when I click on it it shows nothing (empty string).
From what I understand, \ is used to escape special characters, but I didn’t find any information about "\0".

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

What is the best approach to get rid of all "empty but not empty" values like this one?

>Solution :

It’s basically U+0000 – the "null" character. (See the documentation of C# escape sequences for a complete list.) You can just use string.Replace as normal:

string filtered = original.Replace("\0", "");
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