简体   繁体   English

如何检查字符串是否为空

[英]How to check if String is null

I am wondering if there is a special method/trick to check if a String object is null.我想知道是否有特殊的方法/技巧来检查String对象是否为空。 I know about the String.IsNullOrEmpty method but I want to differentiate a null String from an empty String (= "" ).我知道String.IsNullOrEmpty方法,但我想区分null字符串和空字符串 (= "" )。

Should I simply use:我应该简单地使用:

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

...or is there another way? ……或者还有别的方法吗?

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. s的值不是一个对象 - 它是一个reference ,它要么是 null 要么是指一个对象。

And yes, you should just use是的,你应该只使用

if (s == null)

Note that this will still use the overloaded == operator defined in string, but that will do the right thing.请注意,这仍将使用字符串中定义的重载 == 运算符,但这会做正确的事情。

To sure, you should use function to check is null and empty as below:当然,您应该使用函数来检查是否为空和空,如下所示:

string str = ...
if (!String.IsNullOrEmpty(str))
{
...
}

You can use the null coalescing double question marks to test for nulls in a string or other nullable value type:您可以使用空合并双问号来测试字符串或其他可为空值类型中的空值:

textBox1.Text = s ?? "Is null";

The operator '??'运营商 '??' asks if the value of 's' is null and if not it returns 's';询问“s”的值是否为空,如果不是则返回“s”; if it is null it returns the value on the right of the operator.如果为空,则返回运算符右侧的值。

More info here: https://msdn.microsoft.com/en-us/library/ms173224.aspx更多信息: https : //msdn.microsoft.com/en-us/library/ms173224.aspx

And also worth noting there's a null-conditional operator ?.还值得注意的是有一个空条件运算符 ?。 and ?[ introduced in C# 6.0 (and VB) in VS2015和 ?[ 在 VS2015 中的 C# 6.0(和 VB)中引入

textBox1.Text = customer?.orders?[0].description ?? "n/a";

This returns "n/a" if description is null, or if the order is null, or if the customer is null, else it returns the value of description.如果描述为空,或者订单为空,或者客户为空,则返回“n/a”,否则返回描述的值。

More info here: https://msdn.microsoft.com/en-us/library/dn986595.aspx更多信息: https : //msdn.microsoft.com/en-us/library/dn986595.aspx

For .net 5 (probably also for .net Core 3.1)对于 .net 5(可能也适用于 .net Core 3.1)

Different possibility to write but always the same problem.编写的可能性不同,但问题始终相同。

string wep = test ?? "replace";
Console.WriteLine(wep);

result: "replace"结果:“替换”

or或者

string test=null;
test ??= "replace";
Console.WriteLine(test);
test="";
test??="replace";
Console.WriteLine(test);
  • first try: "replace"第一次尝试:“替换”
  • second try: blank第二次尝试:空白
string test="";
if(test is null)
    Console.WriteLine("yaouh");
else
    Console.WriteLine("Not yahouu");

Result: "Not yahou"结果:“不是雅虎”

If you are using C# 7.0 or above you can use is null :如果您使用的是C# 7.0或更高版本,则可以使用is null

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

Also, note that when working with strings you might consider also using IsNullOrWhiteSpace that will also validate that the string doesn't contain only spaces.另外,请注意,在处理字符串时,您可能还考虑使用IsNullOrWhiteSpace ,它也将验证字符串是否仅包含空格。

You can check with null or Number.您可以使用 null 或 Number 进行检查。

First, add a reference to Microsoft.VisualBasic in your application.首先,在您的应用程序中添加对Microsoft.VisualBasic的引用。

Then, use the following code:然后,使用以下代码:

bool b = Microsoft.VisualBasic.Information.IsNumeric("null");
bool c = Microsoft.VisualBasic.Information.IsNumeric("abc");

In the above, b and c should both be false .在上面, bc都应该是false

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

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