在 Java 中检查字符串是否为空的正确方式是什么?
我们可以使用 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