如何在 Java 中檢查字串是否為空?
isEmpty() 方法是 String 類的一個方法,如果當前字串的長度為 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
廣告