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

Time:12-16

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".

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

CodePudding user response:

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", "");
  •  Tags:  
  • c#
  • Related