String.IsNullOrWhiteSpace(String) 方法

定义

指示指定的字符串是 null、空还是仅由空白字符组成。Indicates whether a specified string is null, empty, or consists only of white-space characters.

public:
 static bool IsNullOrWhiteSpace(System::String ^ value);
public static bool IsNullOrWhiteSpace (string value);
static member IsNullOrWhiteSpace : string -> bool
Public Shared Function IsNullOrWhiteSpace (value As String) As Boolean

参数

value
String

要测试的字符串。The string to test.

返回

如果 true 参数为 valuenull,或者如果 Empty 仅由空白字符组成,则为 valuetrue if the value parameter is null or Empty, or if value consists exclusively of white-space characters.

示例

下面的示例创建一个字符串数组,然后将数组的每个元素传递到 IsNullOrWhiteSpace 方法。The following example creates a string array, and then passes each element of the array to the IsNullOrWhiteSpace method.

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { null, String.Empty, "ABCDE", 
                          new String(' ', 20), "  \t   ", 
                          new String('\u2000', 10) };
      foreach (string value in values)
         Console.WriteLine(String.IsNullOrWhiteSpace(value));
   }
}
// The example displays the following output:
//       True
//       True
//       False
//       True
//       True
//       True
Module Example
   Public Sub Main()
      Dim values() As String = { Nothing, String.Empty, "ABCDE", 
                                 New String(" "c, 20), "  " + vbTab + "   ", 
                                 New String(ChrW(&h2000), 10) }
      For Each value As String In values
         Console.WriteLine(String.IsNullOrWhiteSpace(value))
      Next
   End Sub
End Module
' The example displays the following output:
'       True
'       True
'       False
'       True
'       True
'       True

注解

IsNullOrWhiteSpace 是一种简便的方法,类似于下面的代码,但它提供了优异的性能: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;
Return String.IsNullOrEmpty(value) OrElse value.Trim().Length = 0

空白字符由 Unicode 标准定义。White-space characters are defined by the Unicode standard. 当将值作为空白字符传递到 Char.IsWhiteSpace 方法时,IsNullOrWhiteSpace 方法解释返回值 true 的任何字符。The IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character.

适用于

另请参阅