to Lower Case for substring and handle null value - Android java.lang

Android examples for java.lang:String Substring

Description

to Lower Case for substring 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{

    /**//from  w  w w .j ava  2  s  .c om
     * <pre>
     * StringUtil.toLowerCase(null)  = null
     * StringUtil.toLowerCase("han") = "han"
     * StringUtil.toLowerCase("hAn") = "han"
     * </pre>
     * 
     */
    public static String toLowerCase(String str) {
        if (str == null) {
            return null;
        }
        return str.toLowerCase();
    }
    /**
     * 
     * <pre>
     * StringUtil.toLowerCase(null, *, *)  = null
     * StringUtil.toLowerCase("HAN", 0, 1) = "hAN"
     * StringUtil.toLowerCase("HAN", 0, 2) = "haN"
     * StringUtil.toLowerCase("HAN", 0, 3) = "han"
     * </pre>
     * 
     */
    public static String toLowerCase(String str, int beginIndex,
            int endIndex) {
        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        if (beginIndex < 0) {
            beginIndex = 0;
        }
        if (endIndex > str.length()) {
            endIndex = str.length();
        }
        if (beginIndex > 0) {
            sb.append(str.substring(0, beginIndex));
        }
        sb.append(str.substring(beginIndex, endIndex).toLowerCase());
        if (endIndex < str.length()) {
            sb.append(str.substring(endIndex));
        }
        return sb.toString();
    }

}

Related Tutorials