Java String Empty to Null emptyStringToNull(String string)

Here you can find the source of emptyStringToNull(String string)

Description

Returns null, if the specified string is null or the empty string.

License

Apache License

Parameter

Parameter Description
string the string

Return

null if the specified string is null or the empty string, the specified string otherwise

Declaration

public static String emptyStringToNull(String string) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   ww w  . j  a  v a2 s  .co m
     * Returns <code>null</code>, if the specified string is <code>null</code> or
     * the empty string. Returns the specified string otherwise.
     * @param string the string
     * @return <code>null</code> if the specified string is <code>null</code> or
     *         the empty string, the specified string otherwise
     */
    public static String emptyStringToNull(String string) {
        return isEmptyOrNull(string) ? null : string;
    }

    /**
     * Returns if the specified string is <code>null</code> or
     * the empty string.
     * @param string the string
     * @return <code>true</code> if the specified string is <code>null</code> or
     *         the empty string, <code>false</code> otherwise
     */
    public static boolean isEmptyOrNull(String string) {
        return (null == string) || (0 >= string.length());
    }
}

Related

  1. emptyStringToNull(String input)
  2. emptyStringToNull(String str)
  3. emptyToDefault(String value, String def)
  4. emptyToDefault(T[] values, T[] def)
  5. emptyToNull(final String text, final boolean doTrim)
  6. emptyToNull(String field)