如何在 Java 中检查字符串是否为空?

java 8object oriented programmingprogramming更新于 2025/4/24 7:52:17

如果当前字符串的长度为 0,String 类的 isEmpty() 方法将返回 true。

示例

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "tutorialspoint";

      //打印字符串的长度
      System.out.println("length of string = " + str.length());

      //检查字符串是否为空
      System.out.println("is this string empty? = " + str.isEmpty());
   }
}

输出

length of string = 14
is this string empty? = false

相关文章