Java中檢查Null、空或僅為空白字串。


我們可以使用String 類的 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

更新於:30-Jul-2019

2K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.