trim string and handle null value - Android java.lang

Android examples for java.lang:String Strip

Description

trim string and handle null value

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**//w w w  .  ja  v  a  2 s  .c  o  m
     * 
     * <pre>
     * StringUtil.trim(null)    = null
     * StringUtil.trim("")      = ""
     * StringUtil.trim("   ")   = ""
     * StringUtil.trim("han")   = "han"
     * StringUtil.trim(" han ") = "han"
     * </pre>
     * 
     */
    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

}

Related Tutorials