can't call String.isEmpty() in android

JavaAndroid

Java Problem Overview


In my android application I can't use String.isEmpty() function which is situated in JDK 1.6. Android 2.1 lib doesn't have this function in java.lang.String class

I tried to input JRE System library to my project, because it has this function, but there was no effects.

How can I solve this problem and allow my application to use this function?

Java Solutions


Solution 1 - Java

You can use android.text.TextUtils.isEmpty() instead. This method also checks to see if the String is null and has been available since API level 1.

if (TextUtils.isEmpty(str)) {
    Log.d(TAG, "String is empty or null!");
}

Solution 2 - Java

> How can I solve this problem and allow my application to use this function?

You can't.

Use String.length() == 0 instead. It is backwards compatible all the way back to JDK 1.0 ... and with J2ME as well.

String.equals("") is another alternative.


> Are you sure that there is no way to configure Eclipse to put into a code classes from definite libraries?

Not if you want your app to run on a real Android device. Java / Android platforms intentionally make it hard for you to tinker with the behaviour of the core class libraries. For a start, you can only do it by modifying the Davlik equivalent of the bootclasspath or rt.jar file, and neither of these can be done within a running JVM.

That kind of tinkering has the risk of potentially breaking Java for other apps. Even assuming that you can't compromise Android app separation directly (because of the process/uid separation mentioned below), malicious tweaks to the (shared) Java core classes could still potentially allow one app to interfere with, or steal information from another app.

Solution 3 - Java

this method appeared in API9, so you cant use it before Android2.3

Solution 4 - Java

You can use this TextUtils.isEmpty(str). This is available since API level 1 or You can create you own method like below

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

Solution 5 - Java

as far as I know android supports java 5 , so there is no isEmpty(); you can use length() to simulate isEmpty()

Solution 6 - Java

"".equals(yourString); also gives the same behavior like String.isEmpty();

Solution 7 - Java

You have to upgrade android API to level 9, or use String.trim().length()==0 or String.equals("") instead, it should work for your android API level and your JDK version.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionteoREtikView Question on Stackoverflow
Solution 1 - JavatwaddingtonView Answer on Stackoverflow
Solution 2 - JavaStephen CView Answer on Stackoverflow
Solution 3 - JavakzotinView Answer on Stackoverflow
Solution 4 - JavaZeeshanView Answer on Stackoverflow
Solution 5 - JavajmjView Answer on Stackoverflow
Solution 6 - JavaMuhammad Aamir AliView Answer on Stackoverflow
Solution 7 - JavaEngine BaiView Answer on Stackoverflow