Convert object to string and handle null value - Android java.lang

Android examples for java.lang:String Convert

Description

Convert object to string and handle null value

Demo Code

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.security.SecureRandom;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Main{

    public static String nullConvert(Object src) {
        if (src != null && src instanceof java.math.BigDecimal) {
            return ((BigDecimal) src).toString();
        }/*from  ww w  . j a  v a 2s. c om*/

        if (src == null || src.equals("null")) {
            return "";
        } else {
            return ((String) src).trim();
        }
    }

    public static String nullConvert(String src) {

        if (src == null || src.equals("null") || "".equals(src)
                || " ".equals(src)) {
            return "";
        } else {
            return src.trim();
        }
    }

}

Related Tutorials