Java Android TextUtils isEmpty 与 String.isEmpty

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

Android TextUtils isEmpty vs String.isEmpty

javaandroid

提问by Suri

What is difference between TextUtils.isEmpty(string)and string.isEmpty?

TextUtils.isEmpty(string)和 和有什么不一样string.isEmpty

Both do the same operation.

两者都做同样的操作。

Is it advantageous to use TextUtils.isEmpty(string)?

使用有好处TextUtils.isEmpty(string)吗?

回答by OneCricketeer

Yes, TextUtils.isEmpty(string)is preferred.

是的,TextUtils.isEmpty(string)是首选。



For string.isEmpty(), a null string value will throw a NullPointerException

对于string.isEmpty(),空字符串值将抛出NullPointerException

TextUtilswill always return a boolean value.

TextUtils将始终返回一个布尔值。

In code, the former simply calls the equivalent of the other, plus a null check.

在代码中,前者简单地调用了 other 的等价物,再加上一个空检查。

return string == null || string.length() == 0;

回答by Ahmed Mostafa

In class TextUtils

在班上 TextUtils

public static boolean isEmpty(@Nullable CharSequence str) {
    if (str == null || str.length() == 0) {
        return true;
    } else {
        return false;
    }
}

checks if string length is zero and if string is null to avoid throwing NullPointerException

检查字符串长度是否为零以及字符串是否为空以避免抛出 NullPointerException

in class String

在班上 String

public boolean isEmpty() {
    return count == 0;
}

checks if string length is zero only, this may result in NullPointerExceptionif you try to use that string and it is null.

检查字符串长度是否仅为零,NullPointerException如果您尝试使用该字符串并且它为空,这可能会导致。

回答by ΦXoc? ? Пepeúpa ツ

Take a look at the doc

看一下文档

for the String#isEmptythey specify:

对于String#isEmpty他们指定:

boolean
isEmpty() Returns true if, and only if, length() is 0.

boolean
isEmpty() 当且仅当 length() 为 0 时返回 true。

and for the TextUtils.isEmptythe documentation explains:

对于TextUtils.isEmpty文档说明:

public static boolean isEmpty (CharSequence str)

Returns true if the string is null or 0-length.

public static boolean isEmpty (CharSequence str)

如果字符串为空或长度为 0,则返回 true。

so the main difference is that using the TextUtils.isEmpty, you dont care or dont need to check if the string is null referenced or not,

所以主要的区别是使用 TextUtils.isEmpty,你不关心或不需要检查字符串是否为空引用,

in the other case yes.

在另一种情况下是的。

回答by Wackaloon

TextUtils.isEmpty()is better in Android SDK because of inner null check, so you don't need to check string for null before checking its emptiness yourself.

TextUtils.isEmpty()由于内部空检查,在Android SDK中更好,因此您无需在自己检查空值之前检查字符串是否为空。

But with Kotlin, you can use String?.isEmpty()and String?.isNotEmpty()instead of TextUtils.isEmpty()and !TextUtils.isEmpty(), it will be more reader friendly

但是在 Kotlin 中,你可以使用String?.isEmpty()andString?.isNotEmpty()代替TextUtils.isEmpty()and !TextUtils.isEmpty(),它会更友好

So I think it is preferred to use String?.isEmpty()in Kotlin and TextUtils.isEmpty()in Android Java SDK

所以我认为最好String?.isEmpty()在 Kotlin 和TextUtils.isEmpty()Android Java SDK 中使用

回答by Yagna

String?.isNullOrEmpty

might be what you are looking for

可能是您正在寻找的