如何在 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

更新于: 2020-2-26

243 次浏览

开启您的职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.