Check whether a string is not null and not empty

后端 未结 30 2097
予麋鹿
予麋鹿 2020-11-22 02:13

How can I check whether a string is not null and not empty?

public void doStuff(String str)
{
    if (str != null && str != \"**here I want to check          


        
相关标签:
30条回答
  • 2020-11-22 02:48

    If you use Spring framework then you can use method:

    org.springframework.util.StringUtils.isEmpty(@Nullable Object str);
    

    This method accepts any Object as an argument, comparing it to null and the empty String. As a consequence, this method will never return true for a non-null non-String object.

    0 讨论(0)
  • 2020-11-22 02:49

    Just adding Android in here:

    import android.text.TextUtils;
    
    if (!TextUtils.isEmpty(str)) {
    ...
    }
    
    0 讨论(0)
  • 2020-11-22 02:51

    Returns true or false based on input

    Predicate<String> p = (s)-> ( s != null && !s.isEmpty());
    p.test(string);
    
    0 讨论(0)
  • 2020-11-22 02:53

    What about isEmpty() ?

    if(str != null && !str.isEmpty())
    

    Be sure to use the parts of && in this order, because java will not proceed to evaluate the second part if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.

    Beware, it's only available since Java SE 1.6. You have to check str.length() == 0 on previous versions.


    To ignore whitespace as well:

    if(str != null && !str.trim().isEmpty())
    

    (since Java 11 str.trim().isEmpty() can be reduced to str.isBlank() which will also test for other Unicode white spaces)

    Wrapped in a handy function:

    public static boolean empty( final String s ) {
      // Null-safe, short-circuit evaluation.
      return s == null || s.trim().isEmpty();
    }
    

    Becomes:

    if( !empty( str ) )
    
    0 讨论(0)
  • 2020-11-22 02:53

    Almost every library I know defines a utility class called StringUtils, StringUtil or StringHelper, and they usually include the method you are looking for.

    My personal favorite is Apache Commons / Lang, where in the StringUtils class, you get both the

    1. StringUtils.isEmpty(String) and the
    2. StringUtils.isBlank(String) method

    (The first checks whether a string is null or empty, the second checks whether it is null, empty or whitespace only)

    There are similar utility classes in Spring, Wicket and lots of other libs. If you don't use external libraries, you might want to introduce a StringUtils class in your own project.


    Update: many years have passed, and these days I'd recommend using Guava's Strings.isNullOrEmpty(string) method.

    0 讨论(0)
  • 2020-11-22 02:54

    If you don't want to include the whole library; just include the code you want from it. You'll have to maintain it yourself; but it's a pretty straight forward function. Here it is copied from commons.apache.org

        /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题