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


如果當前字串的長度為 0,String 類的 isEmpty() 方法將返回 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 日

245 次瀏覽

開啟你的 職業 生涯

透過完成課程獲得認證

開始
廣告