如何測試字串是否為空或空字串?


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

更新於: 2020-02-26

494 次瀏覽

開始你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.