如何在 Java 中檢查字串是否為空?


isEmpty() 方法是 String 類的一個方法,如果當前字串的長度為 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

更新於: 2020-2-26

243 次瀏覽

開啟您的職業生涯

完成課程並獲得認證

開始
廣告