閱讀英文

共用方式為


String.IsNullOrWhiteSpace(String) 方法

定義

表示指定的字串是否為 null、空白,或只由空白字元組成的字串。

public static bool IsNullOrWhiteSpace(string value);
public static bool IsNullOrWhiteSpace(string? value);

參數

value
String

要測試的字串。

傳回

如果 true 參數是 valuenull,或者 Empty 完全由空白字元組成,則為 value

範例

下列範例會建立字串陣列,然後將陣列的每個元素傳遞至 IsNullOrWhiteSpace 方法。

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

備註

IsNullOrWhiteSpace 是類似下列程式碼的便利方法,不同之處在于它提供較佳的效能:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;

空白字元是由 Unicode 標準所定義。 方法 IsNullOrWhiteSpace 會將傳回 值 true 的任何字元解譯為空白字元時傳回 Char.IsWhiteSpace 的值。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱