如何检查是否字符串为空 [英] How to check if String is null

查看:188
本文介绍了如何检查是否字符串为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种特殊的方法/诀窍来检查字符串对象为null。我了解 String.IsNullOrEmpty 方法,但我想区分一个空字符串一个字符串(=

I am wondering if there is a special method/trick to check if a String object is null. I know about the String.IsNullOrEmpty method but I want to differentiate a null String from an empty String (="").

我应该简单地使用:

if (s == null) {
    // blah blah...
}

...或有另一种方式

...or is there another way?

推荐答案

这是对象的不能为null - 一个值表达式的可以为null。这是值得的在你的心中的区别清楚。值取值不是一个对象 - 这是一个的参考的,这是null或引用对象

An object can't be null - the value of an expression can be null. It's worth making the difference clear in your mind. The value of s isn't an object - it's a reference, which is either null or refers to an object.

是的,你应该只使用

if (s == null)

请注意,这仍然会使用字符串中定义的重载==操作符,但会做正确的事。

Note that this will still use the overloaded == operator defined in string, but that will do the right thing.

这篇关于如何检查是否字符串为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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