String starts With and handle null value - Android java.lang

Android examples for java.lang:String Starts or Ends

Description

String starts With 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 ww w .  ja v a  2s  .  c  om
     * <pre>
     * StringUtil.startsWith(null, *)    = false
     * StringUtil.startsWith(*, null)    = false
     * StringUtil.startsWith("han", "h") = true
     * StringUtil.startsWith("han", "a") = false
     * </pre>
     * @return
     */
    public static boolean startsWith(String str, String prefix) {
        if (str == null || prefix == null) {
            return false;
        }
        return str.startsWith(prefix);
    }
    /**
     * <pre>
     * StringUtil.startsWith(null, *, 0)    = false
     * StringUtil.startsWith(*, null, 0)    = false
     * StringUtil.startsWith("han", "h", 0) = true
     * StringUtil.startsWith("han", "a", 0) = false
     * StringUtil.startsWith("han", "a", 1) = true
     * </pre>
     * @return
     */
    public static boolean startsWith(String str, String prefix, int offset) {
        if (str == null || prefix == null) {
            return false;
        }
        return str.startsWith(prefix, offset);
    }

}

Related Tutorials