Java String Starts Wtih startsWith(String str, String prefix, boolean ignoreCase)

Here you can find the source of startsWith(String str, String prefix, boolean ignoreCase)

Description

Check if a String starts with a specified prefix (optionally case insensitive).

License

Open Source License

Parameter

Parameter Description
str the String to check, may be null
prefix the prefix to find, may be null
ignoreCase inidicates whether the compare should ignore case (case insensitive) or not.

Return

true if the String starts with the prefix or both null

Declaration

private static boolean startsWith(String str, String prefix, boolean ignoreCase) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w w w  . j a v  a2  s  . c  om
     * <p>Check if a String starts with a specified prefix.</p>
     * <p/>
     * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
     * references are considered to be equal. The comparison is case sensitive.</p>
     * <p/>
     * <pre>
     * StrUtils.startsWith(null, null)      = true
     * StrUtils.startsWith(null, "abcdef")  = false
     * StrUtils.startsWith("abc", null)     = false
     * StrUtils.startsWith("abc", "abcdef") = true
     * StrUtils.startsWith("abc", "ABCDEF") = false
     * </pre>
     *
     * @param str    the String to check, may be null
     * @param prefix the prefix to find, may be null
     * @return <code>true</code> if the String starts with the prefix, case sensitive, or
     * both <code>null</code>
     * @see String#startsWith(String)
     * @since 2.4
     */
    public static boolean startsWith(String str, String prefix) {
        return startsWith(str, prefix, false);
    }

    /**
     * <p>Check if a String starts with a specified prefix (optionally case insensitive).</p>
     *
     * @param str        the String to check, may be null
     * @param prefix     the prefix to find, may be null
     * @param ignoreCase inidicates whether the compare should ignore case
     *                   (case insensitive) or not.
     * @return <code>true</code> if the String starts with the prefix or
     * both <code>null</code>
     * @see String#startsWith(String)
     */
    private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
        if (str == null || prefix == null) {
            return (str == null && prefix == null);
        }
        return prefix.length() <= str.length() && str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
    }

    /**
     * Gets a String's length or <code>0</code> if the String is <code>null</code>.
     *
     * @param str a String or <code>null</code>
     * @return String length or <code>0</code> if the String is <code>null</code>.
     * @since 2.4
     */
    public static int length(String str) {
        return str == null ? 0 : str.length();
    }
}

Related

  1. startsWith(String str, String mark, int paramInt)
  2. startsWith(String str, String prefix)
  3. startsWith(String str, String prefix)
  4. StartsWith(String str, String prefix)
  5. startsWith(String str, String prefix)
  6. startsWith(String str, String prefix, boolean ignoreCase)
  7. startsWith(String str, String prefix, int index)
  8. startsWith(String str, String start, boolean caseSensitive)
  9. startsWith(String str1Raw, String str2Raw)