使用 Java 檢測 Null 或者為空或僅包含空白字元的字串。


我們可以使用 String 類中的 isEmpty()方法來驗證給定字串是否為空。只有當 length() 為 0 時,此方法才返回 true。

示例

 現場演示

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "tutorialspoint";
      // prints length of string
      System.out.println("length of string = " + str.length());
      // checks if the string is empty or not
      System.out.println("is this string empty? = " + str.isEmpty());
   }
}

輸出

length of string = 14
is this string empty? = false

更新日期:2019-07-30

2K+ 次觀看

啟程你的 職業生涯

完成課程認證

開始
廣告
© . All rights reserved.