简体   繁体   English

如何删除所有转义字符(不是特殊字符)

[英]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 .我正在解析一些 WMI 查询结果,一些在 Powershell 的Get-CimInstance中为空的值从 C# 的ManagementObjectSearcher中获取"\0"的值。

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).s在调试器中显示为"\0" ,但是当我单击它时,它什么也不显示(空字符串)。 From what I understand, \ is used to escape special characters, but I didn't find any information about "\0" .据我了解, \用于转义特殊字符,但我没有找到有关"\0"的任何信息。

What is the best approach to get rid of all "empty but not empty" values like this one?摆脱像这样的所有“空但非空”值的最佳方法是什么?

It's basically U+0000 - the "null" character.它基本上是 U+0000 - “空”字符。 (See the documentation of C# escape sequences for a complete list.) You can just use string.Replace as normal: (有关完整列表,请参阅C# 转义序列的文档。)您可以正常使用string.Replace

string filtered = original.Replace("\0", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM