Java String Last Index Of lastIndexOfIgnoreCase(String pString, String pLookFor)

Here you can find the source of lastIndexOfIgnoreCase(String pString, String pLookFor)

Description

Returns the index within this string of the rightmost occurrence of the specified substring.

License

Open Source License

Parameter

Parameter Description
pString The string to test
pLookFor The string to look for

Return

If the string argument occurs one or more times as a substring within this object at a starting index no greater than fromIndex, then the index of the first character of the last such substring is returned. If it does not occur as a substring starting at fromIndex or earlier, -1 is returned.

Declaration

public static int lastIndexOfIgnoreCase(String pString, String pLookFor) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w w .  java  2s .co  m
     * Returns the index within this string of the rightmost occurrence of the
     * specified substring. The rightmost empty string "" is considered to
     * occur at the index value {@code pString.length() - 1}.
     *
     * @param pString  The string to test
     * @param pLookFor The string to look for
     * @return If the string argument occurs one or more times as a substring
     *         within this object at a starting index no greater than fromIndex, then
     *         the index of the first character of the last such substring is returned.
     *         If it does not occur as a substring starting at fromIndex or earlier, -1
     *         is returned.
     * @see String#lastIndexOf(String)
     */
    public static int lastIndexOfIgnoreCase(String pString, String pLookFor) {
        return lastIndexOfIgnoreCase(pString, pLookFor, pString != null ? pString.length() - 1 : -1);
    }

    /**
     * Returns the index within this string of the rightmost occurrence of the
     * specified substring. The rightmost empty string "" is considered to
     * occur at the index value {@code pPos}
     *
     * @param pString  The string to test
     * @param pLookFor The string to look for
     * @param pPos     The last index to test
     * @return If the string argument occurs one or more times as a substring
     *         within this object at a starting index no greater than fromIndex, then
     *         the index of the first character of the last such substring is returned.
     *         If it does not occur as a substring starting at fromIndex or earlier, -1
     *         is returned.
     * @see String#lastIndexOf(String,int)
     */
    public static int lastIndexOfIgnoreCase(String pString, String pLookFor, int pPos) {
        if ((pString == null) || (pLookFor == null)) {
            return -1;
        }
        if (pLookFor.length() == 0) {
            return pPos;// All strings "contains" the empty string
        }
        if (pLookFor.length() > pString.length()) {
            return -1;// Cannot contain string longer than itself
        }

        // Get first char
        char firstL = Character.toLowerCase(pLookFor.charAt(0));
        char firstU = Character.toUpperCase(pLookFor.charAt(0));
        int indexLower = pPos;
        int indexUpper = pPos;

        for (int i = pPos; i >= 0; i--) {

            // Peek for first char
            indexLower = ((indexLower >= 0) && (indexLower >= i)) ? pString.lastIndexOf(firstL, i) : indexLower;
            indexUpper = ((indexUpper >= 0) && (indexUpper >= i)) ? pString.lastIndexOf(firstU, i) : indexUpper;
            if (indexLower < 0) {
                if (indexUpper < 0) {
                    return -1;// First char not found
                } else {
                    i = indexUpper;// Only upper
                }
            } else if (indexUpper < 0) {
                i = indexLower;// Only lower
            } else {

                // Both found, select last occurence
                i = (indexLower > indexUpper) ? indexLower : indexUpper;
            }

            // Only one?
            if (pLookFor.length() == 1) {
                return i;// The only char found!
            }

            // Test if we still have enough chars
            else if (i > (pString.length() - pLookFor.length())) {
                //return -1;
                continue;
            }

            // Test if last char equals! (regionMatches is expensive)
            else if ((pString.charAt(i + pLookFor.length() - 1) != Character
                    .toLowerCase(pLookFor.charAt(pLookFor.length() - 1)))
                    && (pString.charAt(i + pLookFor.length() - 1) != Character
                            .toUpperCase(pLookFor.charAt(pLookFor.length() - 1)))) {
                continue;// Nope, try next
            }

            // Test from second char, until second-last char
            else if ((pLookFor.length() <= 2)
                    || pString.regionMatches(true, i + 1, pLookFor, 1, pLookFor.length() - 2)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Returns the index within this string of the last occurrence of the
     * specified character.
     *
     * @param pString The string to test
     * @param pChar   The character to look for
     * @return if the string argument occurs as a substring within this object,
     *         then the index of the first character of the first such substring is
     *         returned; if it does not occur as a substring, -1 is returned.
     * @see String#lastIndexOf(int)
     */
    public static int lastIndexOfIgnoreCase(String pString, int pChar) {
        return lastIndexOfIgnoreCase(pString, pChar, pString != null ? pString.length() : -1);
    }

    /**
     * Returns the index within this string of the last occurrence of the
     * specified character, searching backward starting at the specified index.
     *
     * @param pString The string to test
     * @param pChar   The character to look for
     * @param pPos    The last index to test
     * @return if the string argument occurs as a substring within this object,
     *         then the index of the first character of the first such substring is
     *         returned; if it does not occur as a substring, -1 is returned.
     * @see String#lastIndexOf(int,int)
     */
    public static int lastIndexOfIgnoreCase(String pString, int pChar, int pPos) {
        if ((pString == null)) {
            return -1;
        }

        // Get first char
        char lower = Character.toLowerCase((char) pChar);
        char upper = Character.toUpperCase((char) pChar);
        int indexLower;
        int indexUpper;

        // Test for char
        indexLower = pString.lastIndexOf(lower, pPos);
        indexUpper = pString.lastIndexOf(upper, pPos);
        if (indexLower < 0) {

            /*      if (indexUpper < 0)
            return -1; // First char not found
            else */
            return indexUpper;// Only upper
        } else if (indexUpper < 0) {
            return indexLower;// Only lower
        } else {

            // Both found, select last occurence
            return (indexLower > indexUpper) ? indexLower : indexUpper;
        }
    }

    /**
     * Converts a string to lowercase.
     *
     * @param pString the string to convert
     * @return the string converted to lowercase, or null if the argument was
     *         null.
     */
    public static String toLowerCase(String pString) {
        if (pString != null) {
            return pString.toLowerCase();
        }
        return null;
    }

    /**
     * Converts a string to uppercase.
     *
     * @param pString the string to convert
     * @return the string converted to uppercase, or null if the argument was
     *         null.
     */
    public static String toUpperCase(String pString) {
        if (pString != null) {
            return pString.toUpperCase();
        }
        return null;
    }
}

Related

  1. lastIndexOf(StringBuffer buf, String str)
  2. lastIndexOfAny(final String delimiters, final String str)
  3. lastIndexOfAny(String str, String search, int offset)
  4. lastIndexOfAnyBut(String srcString, String validString)
  5. lastIndexOfAnyDelimiter(String str, int fromIndex, String delimiters)
  6. lastIndexOfIgnoreCase(String s, String substr)
  7. lastIndexOfIgnoreCase(String source, String str, int fromIndex)
  8. lastIndexOfIgnoreCase(String str, String searchStr)
  9. lastIndexOfIgnoreCase(String str, String searchStr)