C# IsNullOrWhiteSpace() 方法


C# 中的 IsNullOrWhiteSpace() 方法用於指示指定的字串是否為 null、空或僅由空格符組成。

語法

public static bool IsNullOrWhiteSpace (string val);

上方,val 引數是要測試的字串。現在我們來看一個示例 −

示例

現在我們來看一個示例

 動態演示

using System;
public class Demo {
   public static void Main() {
      string str1 = null;
      string str2 = String.Empty;
      Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
      Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
   }
}

輸出

這將產生以下輸出 -

Is string1 null or whitespace? = True
Is string2 null or whitespace? = True

示例

現在我們來看另一個示例 -

 動態演示

using System;
public class Demo {
   public static void Main() {
      string str1 = "
";       string str2 = "Tim";       Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));       Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));    } }

輸出

Is string1 null or whitespace? = True
Is string2 null or whitespace? = False

更新於: 2019-12-03

216 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.