Java String Starts Wtih startsWithIgnoreCase(final String text, final String prefix)

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

Description

Tests if the string starts with the specified prefix.

License

Open Source License

Parameter

Parameter Description
text the string to test.
prefix the prefix.

Return

true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the prefix is an empty string or is equal to the text String object as determined by the method. If the text or prefix argument is null false is returned.

Declaration

public static boolean startsWithIgnoreCase(final String text, final String prefix) 

Method Source Code

//package com.java2s;
/*//from   www  .  j a  v a  2s  .  c  o m
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

public class Main {
    /**
     * Tests if the string starts with the specified prefix.
     * 
     * @param text the string to test.
     * @param prefix the prefix.
     * @return <code>true</code> if the character sequence represented by the argument is a prefix of the character sequence
     *         represented by this string; <code>false</code> otherwise. Note also that <code>true</code> will be returned if the
     *         prefix is an empty string or is equal to the text <code>String</code> object as determined by the
     *         {@link #equals(Object)} method. If the text or prefix argument is null <code>false</code> is returned.
     * @since JDK1. 0
     */
    public static boolean startsWithIgnoreCase(final String text, final String prefix) {
        if (isEmpty(text)) {
            return false;
        }
        if (prefix == null) {
            return false;
        }
        int textLength = text.length();
        int prefixLength = prefix.length();
        if (prefixLength == 0) {
            return true;
        }
        if (prefixLength > textLength) {
            return false;
        }
        char[] chArray = prefix.toCharArray();
        for (int i = 0; i != chArray.length; ++i) {
            char ch1 = chArray[i];
            char ch2 = text.charAt(i);
            if (ch1 == ch2 || Character.toLowerCase(ch1) == Character.toLowerCase(ch2)) {
                // continue
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>
     * Returns whether the specified text is either empty or null.
     * </p>
     * 
     * @param text The text to check; may be null;
     * @return True if the specified text is either empty or null.
     * @since 4.0
     */
    public static boolean isEmpty(final String text) {
        return (text == null || text.length() == 0);
    }

    public static String toLowerCase(String str) {
        String newStr = convertBasicLatinToLower(str);
        if (newStr == null) {
            return str.toLowerCase();
        }
        return newStr;
    }

    private static String convertBasicLatinToLower(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (isBasicLatinUpperCase(chars[i])) {
                chars[i] = (char) ('a' + (chars[i] - 'A'));
            } else if (!isBasicLatinChar(chars[i])) {
                return null;
            }
        }
        return new String(chars);
    }

    private static boolean isBasicLatinUpperCase(char c) {
        return c >= 'A' && c <= 'Z';
    }

    private static boolean isBasicLatinChar(char c) {
        return c <= '\u007F';
    }
}

Related

  1. startsWithIgnoreCase(final String str, final String prefix)
  2. startsWithIgnoreCase(final String str, final String prefix)
  3. startsWithIgnoreCase(final String string, final String start)
  4. startsWithIgnoreCase(final String stringToCheck, final String prefix)
  5. startsWithIgnoreCase(final String target1, final String target2)
  6. startsWithIgnoreCase(final String text, final String prefix)
  7. startsWithIgnoreCase(final String value, final String possiblePrefix)
  8. startsWithIgnoreCase(String a, String b)
  9. startsWithIgnoreCase(String baseString, String compareString)