C# 中 IsNullOrEmpty 和 IsNullOrWhiteSpace 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18710644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between IsNullOrEmpty and IsNullOrWhiteSpace in C#
提问by Asieh hojatoleslami
What are differences between these commands in C#
C#中这些命令有什么区别
string text= " ";
1-string.IsNullOrEmpty(text.Trim())
2-string.IsNullOrWhiteSpace(text)
采纳答案by fionbio
IsNullOrWhiteSpace
is a convenience method that is similar to the following code, except that it offers superior performance:return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
White-space characters are defined by the Unicode standard. The
IsNullOrWhiteSpace
method interprets any character that returns a value of true when it is passed to theChar.IsWhiteSpace
method as a white-space character.
IsNullOrWhiteSpace
是一种类似于以下代码的便捷方法,不同之处在于它提供了卓越的性能:return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
空白字符由 Unicode 标准定义。该
IsNullOrWhiteSpace
方法将在传递给该Char.IsWhiteSpace
方法时返回值为 true 的任何字符解释为空白字符。
回答by TGH
The first method checks if a string is null or a blank string. In your example you can risk a null reference since you are not checking for null before trimming
第一个方法检查字符串是空字符串还是空字符串。在您的示例中,您可能会冒空引用的风险,因为您在修剪之前没有检查空值
1- string.IsNullOrEmpty(text.Trim())
The second method checks if a string is null or an arbitrary number of spaces in the string (including a blank string)
第二种方法检查字符串是否为空或字符串中的任意数量的空格(包括空字符串)
2- string .IsNullOrWhiteSpace(text)
The method IsNullOrWhiteSpace
covers IsNullOrEmpty
, but it also returns true
if the string contains white space.
该方法IsNullOrWhiteSpace
涵盖IsNullOrEmpty
,但true
如果字符串包含空格,它也会返回。
In your concrete example you should use 2) as you run the risk of a null reference exception in approach 1) since you're calling trim on a string that may be null
在您的具体示例中,您应该使用 2) 因为您在方法 1) 中冒着空引用异常的风险,因为您正在对可能为空的字符串调用 trim
回答by JHubbard80
String.IsNullOrEmpty(string value)
returns true
if the string is null or empty.
For reference an empty string is represented by "" (two double quote characters)
String.IsNullOrEmpty(string value)
true
如果字符串为 null 或为空,则返回。作为参考,空字符串由“”(两个双引号字符)表示
String.IsNullOrWhitespace(string value)
returns true
if the string is null, empty, or contains only whitespace characters such as a space or tab.
String.IsNullOrWhitespace(string value)
返回true
如果字符串是空,空,或仅包含空白字符,例如一个空格或标签。
To see what characters count as whitespace consult this link: http://msdn.microsoft.com/en-us/library/t809ektx.aspx
要查看哪些字符算作空格,请参阅此链接:http: //msdn.microsoft.com/en-us/library/t809ektx.aspx
回答by fubo
Short answer:
简短的回答:
In common use, space " "
, Tab "\t"
and newline "\n"
are the difference:
常用的空格" "
、制表符"\t"
和换行符"\n"
的区别是:
string.IsNullOrWhiteSpace("\t"); //true
string.IsNullOrEmpty("\t"); //false
string.IsNullOrWhiteSpace(" "); //true
string.IsNullOrEmpty(" "); //false
string.IsNullOrWhiteSpace("\n"); //true
string.IsNullOrEmpty("\n"); //false
https://dotnetfiddle.net/4hkpKM
https://dotnetfiddle.net/4hkpKM
also see this answer about: whitespace characters
另请参阅有关以下内容的答案:空白字符
Long answer:
长答案:
There are also a few other white space characters, you probably never used before
还有一些其他的空白字符,您可能以前从未使用过
https://docs.microsoft.com/en-us/dotnet/api/system.char.iswhitespace
https://docs.microsoft.com/en-us/dotnet/api/system.char.iswhitespace
回答by CloudyMarble
If your string (In your case the variable text
) could be null this would make a big Difference:
如果您的字符串(在您的情况下是变量text
)可能为空,这将产生很大的不同:
1-string.IsNullOrEmpty(text.Trim())
--> EXCEPTIONsince your calling a mthode of a null object
1- string.IsNullOrEmpty(text.Trim())
-->异常,因为您调用了空对象的方法
2-string.IsNullOrWhiteSpace(text)
This would work fine since the null issue is beeing checked internally
2-string.IsNullOrWhiteSpace(text)
这可以正常工作,因为正在内部检查 null 问题
To provide the same behaviour using the 1st Option you would have to check somehow if its not null first then use the trim() method
要使用第一个选项提供相同的行为,您必须先检查它是否不为空,然后使用 trim() 方法
if ((text != null) && string.IsNullOrEmpty(text.Trim())) { ... }
回答by ?ubo? ?urgó
This is implementation of methodsafter decompiling.
这是反编译后方法的实现。
public static bool IsNullOrEmpty(String value)
{
return (value == null || value.Length == 0);
}
public static bool IsNullOrWhiteSpace(String value)
{
if (value == null) return true;
for(int i = 0; i < value.Length; i++) {
if(!Char.IsWhiteSpace(value[i])) return false;
}
return true;
}
So it is obvious that IsNullOrWhiteSpacemethod also checks if value that is being passed contain white spaces.
所以很明显IsNullOrWhiteSpace方法还检查传递的值是否包含空格。
Whitespaces refer : https://msdn.microsoft.com/en-us/library/system.char.iswhitespace(v=vs.110).aspx
空格是指:https://msdn.microsoft.com/en-us/library/system.char.iswhitespace( v=vs.110).aspx
回答by vibs2006
string.isNullOrWhiteSpace(text)should be used in most cases as it also includes a blank string.
string.isNullOrWhiteSpace(text)应该在大多数情况下使用,因为它也包含一个空白 string。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
var str = "";
Console.WriteLine(string.IsNullOrWhiteSpace(str));
}
}
}
It returns True!
它返回True!
回答by Italo Pacheco
[Performance Test] just in case anyone is wondering, in a stopwatch test comparing
[性能测试] 以防万一有人想知道,在秒表测试中比较
if(nopass.Trim().Length > 0)
if(nopass.Trim().Length > 0)
if (!string.IsNullOrWhiteSpace(nopass))
if (!string.IsNullOrWhiteSpace(nopass))
these were the results:
结果如下:
Trim-Length with empty value = 15
修剪长度为空值 = 15
Trim-Length with not empty value = 52
修剪长度不为空值 = 52
IsNullOrWhiteSpace with empty value = 11
IsNullOrWhiteSpace 空值 = 11
IsNullOrWhiteSpace with not empty value = 12
IsNullOrWhiteSpace 非空值 = 12