如何測試String是否為null或空?


我們可以使用字串類的isEmpty()方法驗證給定的字串是否為空。此方法僅在長度()為 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 日

494 次檢視

開啟你的職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.