Java 中的字符串 isNullOrEmpty?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2272169/
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-13 05:31:16  来源:igfitidea点击:

String isNullOrEmpty in Java?

javastring

提问by ripper234

This surely has been asked before, but Googling doesn't find it. Is there, in any of the standard java libraries (including apache/google/...), a static isNullOrEmpty()method for Strings?

这肯定以前有人问过,但谷歌搜索没有找到。在任何标准 Java 库(包括 apache/google/...)中,是否有静态isNullOrEmpty()方法Strings

采纳答案by Bozho

from Apache commons-lang.

来自 Apache commons-lang

The difference between emptyand blankis : a string consisted of whitespaces only is blankbut isn't empty.

empty和之间的区别blank是:仅由空格组成的字符串 isblank而不是empty

I generally prefer using apache-commons if possible, instead of writing my own utility methods, although that is also plausible for simple ones like these.

如果可能的话,我通常更喜欢使用 apache-commons,而不是编写我自己的实用程序方法,尽管这对于像这样的简单方法也是合理的。

回答by Jon Skeet

No, which is why so many other libraries have their own copy :)

不,这就是为什么这么多其他图书馆都有自己的副本:)

回答by Vicky

public static boolean isNull(String str) {
        return str == null ? true : false;
    }

    public static boolean isNullOrBlank(String param) {
        if (isNull(param) || param.trim().length() == 0) {
            return true;
        }
        return false;
    }

回答by Brian Deterling

For new projects, I've started having every class I write extend the same base class where I can put all the utility methods that are annoyingly missing from Java like this one, the equivalent for collections (tired of writing list != null && ! list.isEmpty()), null-safe equals, etc. I still use Apache Commons for the implementation but this saves a small amount of typing and I haven't seen any negative effects.

对于新项目,我已经开始让我编写的每个类都扩展相同的基类,我可以在其中放置 Java 中令人讨厌的所有实用方法,就像这样,集合的等价物(厌倦了编写 list != null && ! list.isEmpty())、null-safe equals 等。我仍然使用 Apache Commons 来实现,但这节省了少量的输入,而且我没有看到任何负面影响。

回答by user207421

I've seen this method written a few times in projects I've been on but I have to say I've never written it myself, or called it either ... Generally I find null and empty are completely distinct conditions and I have no reason to ever conflate them.

我已经在我参与的项目中看到这种方法写了几次,但我不得不说我从来没有自己写过它,或者也没有调用过它......通常我发现 null 和 empty 是完全不同的条件,我有没有理由将它们混为一谈。

回答by Peter Lawrey

You can add one

你可以加一个

public static boolean isNullOrBlank(String param) { 
    return param == null || param.trim().length() == 0;
}

I have

我有

public static boolean isSet(String param) { 
    // doesn't ignore spaces, but does save an object creation.
    return param != null && param.length() != 0; 
}

回答by Jarek Przygódzki

com.google.common.base.Strings.isNullOrEmpty(String string)from Google Guava

com.google.common.base.Strings.isNullOrEmpty(String string)来自谷歌番石榴

回答by Zack

In addition to the other answers, I ran across this because I'm a C# programmer primarily, but trying to keep fresh in Java. I noticed that when I tried to use StringUtilsmy IDE (Eclipse) imported it from com.mysql.jdbc.StringUtilswhich actually has an isNullOrEmpty(myStringObject)method.

除了其他答案之外,我遇到了这个问题,因为我主要是一个 C# 程序员,但试图保持 Java 的新鲜度。我注意到当我尝试使用StringUtils我的 IDE (Eclipse) 时,com.mysql.jdbc.StringUtils它实际上有一个isNullOrEmpty(myStringObject)方法导入。

ex.

前任。

import com.mysql.jdbc.StringUtils;

import com.mysql.jdbc.StringUtils;

StringUtils.isNullOrEmpty(host)

StringUtils.isNullOrEmpty(host)

Just another alternative for those who already have the MySQL connector referenced in your project, but not the other StringUtilslibrary.

对于那些已经在您的项目中引用了 MySQL 连接器而不是其他StringUtils库的人来说,这只是另一种选择。

回答by Noah

If you are doing android development, you can use:

如果你在做android开发,你可以使用:

TextUtils.isEmpty (CharSequence str) 

Added in API level 1 Returns true if the string is null or 0-length.

在 API 级别 1 中添加 如果字符串为空或长度为 0,则返回 true。

回答by Lamar

To check if a string got any characters, ie. not null or whitespaces, check StringUtils.hasText-method (if you are using Spring of course)

检查字符串是否有任何字符,即。不是 null 或空格,检查StringUtils.hasText-method (当然,如果您使用的是 Spring)

Example:

例子:

StringUtils.hasText(null) == false
StringUtils.hasText("") == false
StringUtils.hasText(" ") == false
StringUtils.hasText("12345") == true
StringUtils.hasText(" 12345 ") == true