Java isEmpty() 未为字符串定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1483534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:19:29  来源:igfitidea点击:

Java isEmpty() undefined for String?

javaeclipsestring

提问by Aaron Moodie

I'm working on a school project in eclipse, and when I try and use the isEmpty()method on a String, Eclipse shows up the following error:

我正在 eclipse 中处理一个学校项目,当我尝试isEmpty()在 a 上使用该方法时String,Eclipse 显示以下错误:

The method isEmpty() is undefined for the type String

方法 isEmpty() 未定义为 String 类型

I've run the Java updates, but am still getting it. Is any reason why this method would be undefined?

我已经运行了 Java 更新,但仍然得到它。是否有任何原因未定义此方法?

采纳答案by Rich Seller

String.isEmpty() was added in Java 6. In earlier versions you can use StringUtils.isEmpty(String)from Apache's commons-langlibrary.

String.isEmpty() 是在Java 6中添加的。在早期版本中,您可以使用StringUtils.isEmpty(String)Apache的公地郎库。

To configure Eclipse to use the 1.6 JRE, go to Window->Preferences->Java->Installed JREs. If you haven't already got JAva 1.6 configured, select Add...,browse to your 1.6 installation and add it.

要将 Eclipse 配置为使用 1.6 JRE,请转至 Window->Preferences->Java->Installed JREs。如果您还没有配置 JAva 1.6,请选择Add...,浏览到您的 1.6 安装并添加它。

installed JREs screenshot

已安装的 JRE 屏幕截图

configure JREs screenshot
(source: lumidant.com)

配置 JRE 截图
(来源:lumidant.com

回答by Jonik

String.isEmpty()was introduced in Java release 1.6. You might want to check what JDK version you're using for your project. (I don't know much about Eclipse myself, but it should be somewhere in the project settings.)

String.isEmpty()在 Java 1.6 版中引入。您可能想要检查您的项目使用的 JDK 版本。(我自己对 Eclipse 不太了解,但它应该在项目设置中的某个地方。)

回答by dogbane

If you're not on Java 6, String.length() == 0 will return the same result as String.isEmpty().

如果您使用的不是 Java 6,则 String.length() == 0 将返回与 String.isEmpty() 相同的结果。

回答by jlplayel

I had this problem in a big project where all previous set-up were with Java 8 (general and project level compiler settings). The solution was to move to the top The "JRE System Library[jre1.8.0]" in the "Order and Export" tab in Build Path of the affected project.

我在一个大项目中遇到了这个问题,其中所有以前的设置都是使用 Java 8(通用和项目级编译器设置)。解决方案是将受影响项目的Build Path中的“Order and Export”选项卡中的“JRE System Library[jre1.8.0]”移至顶部。