Java - String isEmpty() 方法



描述

Java String isEmpty() 方法用於檢查當前字串是否為空。該方法返回一個布林值,當且僅當字串為空時返回 true;否則返回 false。isEmpty() 方法不接受任何引數。

注意:如果字串初始化為 null,isEmpty() 方法會丟擲 NullPointerException。因為 Null 通常表示“此資料不可用”,它與空字串不同,編譯器無法處理它並丟擲 NullPointerException。

語法

以下是 Java String isEmpty() 方法的語法:

public boolean isEmpty()

引數

  • 該方法不接受任何引數。

返回值

如果 length() 為 0,則此方法返回 true,否則返回 false。

檢查空字串是否為空的示例

如果給定的字串為空,isEmpty() 方法返回true

在下面的程式中,我們建立一個具有值的字串字面量。然後,使用isEmpty() 方法,我們嘗試檢查當前字串是否為空字串。

package com.tutorialspoint;

public class CheckEmpty {
   public static void main(String[] args) {
      
      //create a string literal
      String str = "";
      System.out.println("The given string is an empty." + str);
      System.out.println("Length of the string is: " + str.length());
      
      //using the isEmpty() method
      System.out.println("The current string is an empty string or not? " + str.isEmpty());
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string is an empty.
Length of the string is: 0
The current string is an empty string or not? true

檢查字串是否為空的示例

如果給定的字串非空,此方法返回false

在下面的示例中,我們使用值"TutorialsPoint"例項化字串類。使用isEmpty() 方法,我們嘗試檢查此字串是否為空。

package com.tutorialspoint;

public class CheckEmpty {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      System.out.println("Length of the string is: " + str.length());
      
      //using the isEmpty() method
      System.out.println("The current string is an empty string or not? " + str.isEmpty());
   }
}

輸出

以下是上述程式的輸出:

The given string is: TutorialsPoint
Length of the string is: 14
The current string is an empty string or not? false

檢查字串是否為空的示例

使用條件語句檢查當前字串是否為空。

在這個程式中,我們建立了一個值為"Hello"的字串類物件。然後,使用isEmpty() 方法和條件語句,我們嘗試檢查當前字串是否為空。

package com.tutorialspoint;

public class CheckEmpty {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = new String("Hello");
      System.out.println("The given string is: " + str);
      System.out.println("Length of the string is: " + str.length());
      
      //using the isEmpty() method
      boolean bool = str.isEmpty();
      if(bool) {
         System.out.println("The string is an empty string.");
      } else {
         System.out.println("The string is not an empty string");
      }
   }
}

輸出

上述程式產生以下輸出:

The given string is: Hello
Length of the string is: 5
The string is not an empty string

檢查字串是否為空時出現空指標異常的示例

如果給定的字串為null,此方法將丟擲NullPointerException

在下面的程式中,我們建立一個具有null值的字串字面量。使用isEmpty() 方法,我們嘗試檢查給定的字串是否為空。由於給定的字串為 null,此方法會丟擲異常。

public class CheckEmpty {
   public static void main(String[] args) {
      try {
         
         //create the string literal
         String str = null;
         System.out.println("The given string is: " + str);
         System.out.println("Length of the string is: " + str.length());
         
         //using the isEmpty() method
         boolean bool = str.isEmpty();
         if(bool) {
            System.out.println("The string is an empty string.");
         } else {
            System.out.println("The string is not an empty string");
         }
      } catch(NullPointerException  e) {
         System.out.println("Exception: " + e);
      }
   }
}

輸出

執行上述程式後,將產生以下結果:

The given string is: null
Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
java_lang_string.htm
廣告