为什么string.IsNullOrWhiteSpace(“ \0&”)为假 [英] Why string.IsNullOrWhiteSpace("\0") is false

查看:170
本文介绍了为什么string.IsNullOrWhiteSpace(“ \0&”)为假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,其中不可见的字符 \0 就像一个'空白',未被string.IsNullOrWhiteSpace方法视为空白。我想知道为什么以这种方式在.NET中实现它,并且string.IsNullOrWhiteSpace是否可以正确处理以空字符结尾的字符?
预先感谢。

I faced a problem where invisible character \0 which is pretty like a 'white space' not considered as white space by the string.IsNullOrWhiteSpace method. I wonder why this implemented in .NET in so manner and is there any alternative to string.IsNullOrWhiteSpace which can properly handle null-terminate character? Thanks in advance.

推荐答案

U + 0000 基本上不是空格。 char.IsWhitespace('\0')返回false,未列为空白...

U+0000 isn't whitespace, basically. char.IsWhitespace('\0') returns false, it's not listed as whitespace...

The IsNullOrWhitespace null 部分指的是字符串引用本身,而不是内容,如果那正是您的想法。

The null part of IsNullOrWhitespace refers to the string reference itself - not the contents, if that's what you were thinking of.

请注意,.NET中的字符串在托管代码中不是逻辑上以空终止的,尽管实际上在CLR级别上它们是互操作的。 (字符串知道其自身的长度,但是为了使使用确实期望为空终止符的本机代码更容易使用,CLR确保在字符串内容之后始终有U + 0000 。)如果最后得到一个包含 \0 的字符串,则应该修复产生该字符串的任何内容。

Note that strings in .NET aren't logically "null-terminated" within managed code, although in practice at the CLR level they are, for interop purposes. (The string knows its own length, but in order to make it easier to work with native code which does expect a null terminator, the CLR ensures that there's always a U+0000 after the content of the string.) If you end up with a string containing \0 you should probably fix whatever produced it to start with.

这篇关于为什么string.IsNullOrWhiteSpace(“ \0&”)为假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆