Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextUtils isEmpty vs String.isEmpty

Tags:

java

android

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

Both do the same operation.

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

like image 336
Suri Avatar asked Apr 03 '16 17:04

Suri


People also ask

What is textUtils isEmpty in Android?

isEmpty(CharSequence str) Returns true if the string is null or 0-length. static boolean.

What is textUtils isEmpty?

IsEmpty(ICharSequence) Returns true if the string is null or 0-length. IsEmpty(String) Returns true if the string is null or 0-length.

What is the use of textUtils in Android?

one of the uses of textUtils is for example lets say you have a string "apple,banana,orange,pinapple,mango" which doesnt fit inside a given width it can be converted to "Apple, banana, 2 more" . I like the idea, can you add some code for your answer. provide some code for implementation.

What is string isEmpty?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.


4 Answers

Yes, TextUtils.isEmpty(string) is preferred.


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

TextUtils will always return a boolean value.

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

return string == null || string.length() == 0;
like image 87
OneCricketeer Avatar answered Oct 03 '22 01:10

OneCricketeer


In class 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

in class String

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

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

like image 43
Ahmed Mostafa Avatar answered Oct 03 '22 02:10

Ahmed Mostafa


Take a look at the doc

for the String#isEmpty they specify:

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

and for the TextUtils.isEmpty the documentation explains:

public static boolean isEmpty (CharSequence str)

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

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,

in the other case yes.

like image 40
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 03 '22 02:10

ΦXocę 웃 Пepeúpa ツ


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.

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

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

like image 27
Wackaloon Avatar answered Oct 03 '22 03:10

Wackaloon