Java String Starts Wtih startsWithIgnoreCase(String str, String prefix)

Here you can find the source of startsWithIgnoreCase(String str, String prefix)

Description

Case insensitive check if a String starts with a specified prefix.

nulls are handled without exceptions.

License

Open Source License

Parameter

Parameter Description
str the String to check, may be null
prefix the prefix to find, may be null

Return

true if the String starts with the prefix, case insensitive, or both null

Declaration

public static boolean startsWithIgnoreCase(String str, String prefix) 

Method Source Code

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

public class Main {
    /**/*from   www  .j a  va 2  s .  c o  m*/
     * <p>Case insensitive 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 insensitive.</p>
     * <p/>
     * <pre>
     * StrUtils.startsWithIgnoreCase(null, null)      = true
     * StrUtils.startsWithIgnoreCase(null, "abcdef")  = false
     * StrUtils.startsWithIgnoreCase("abc", null)     = false
     * StrUtils.startsWithIgnoreCase("abc", "abcdef") = true
     * StrUtils.startsWithIgnoreCase("abc", "ABCDEF") = true
     * </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 insensitive, or
     * both <code>null</code>
     * @see String#startsWith(String)
     * @since 2.4
     */
    public static boolean startsWithIgnoreCase(String str, String prefix) {
        return startsWith(str, prefix, true);
    }

    /**
     * <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. startsWithIgnoreCase(String searchIn, int startAt, String searchFor)
  2. startsWithIgnoreCase(String searchIn, String searchFor)
  3. startsWithIgnoreCase(String seq, String prefix)
  4. startsWithIgnoreCase(String startString, String anotherString)
  5. startsWithIgnoreCase(String str, String prefix)
  6. startsWithIgnoreCase(String str, String prefix)
  7. startsWithIgnoreCase(String str, String prefix)
  8. startsWithIgnoreCase(String str, String prefix)
  9. startsWithIgnoreCase(String str, String prefix)