使用 Java 檢測 Null 或者為空或僅包含空白字元的字串。
我們可以使用 String 類中的 isEmpty()方法來驗證給定字串是否為空。只有當 length() 為 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP