Java String Last Index Of lastIndexOfIgnoreCase(String source, String str, int fromIndex)

Here you can find the source of lastIndexOfIgnoreCase(String source, String str, int fromIndex)

Description

Returns the index within this string of the last occurrence of the specified string.

License

Open Source License

Parameter

Parameter Description
source the string to search
str the string to search for
fromIndex the index to start from

Return

the index of the first occurrence of the string in the character sequence represented by this object, or -1 if the string does not occur.

Declaration

public static int lastIndexOfIgnoreCase(String source, String str, int fromIndex) 

Method Source Code

//package com.java2s;
/*//ww w  . j a  va2 s. c o  m
 * ? Copyright IBM Corp. 2012-2013
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

public class Main {
    /**
     * Returns the index within this string of the last occurrence of the
     * specified string.
     * The search is done ignore the character case.
     * @param source the string to search
     * @param str the string to search for
     * @param fromIndex the index to start from
     * @return the index of the first occurrence of the string in the
     *          character sequence represented by this object, or
     *          <code>-1</code> if the string does not occur.
     * @ibm-api
     */
    public static int lastIndexOfIgnoreCase(String source, String str, int fromIndex) {
        int count = source.length();
        int strCount = str.length();

        /*
         * Check arguments; return immediately where possible. For
         * consistency, don't check for null str.
         */
        int rightIndex = count - strCount;
        if (fromIndex < 0) {
            return -1;
        }
        if (fromIndex > rightIndex) {
            fromIndex = rightIndex;
        }
        /* Empty string always matches. */
        if (strCount == 0) {
            return fromIndex;
        }

        int strLastIndex = strCount - 1;
        char strLastChar = Character.toLowerCase(str.charAt(strLastIndex));
        int min = strCount - 1;
        int i = min + fromIndex;

        startSearchForLastChar: while (true) {
            /* Look for the last character */
            while (i >= min && Character.toLowerCase(source.charAt(i)) != strLastChar) {
                i--;
            }
            if (i < min) {
                return -1;
            }

            /* Found last character, now look at the rest of v2. */
            int j = i - 1;
            int start = j - (strCount - 1);
            int k = strLastIndex - 1;

            while (j > start) {
                if (Character.toLowerCase(source.charAt(j--)) != Character.toLowerCase(str.charAt(k--))) {
                    i--;
                    /* Look for str's last char again. */
                    continue startSearchForLastChar;
                }
            }

            return start + 1; /* Found whole string. */
        }
    }

    /**
     * Returns the index within this string of the last occurrence of the
     * specified string.
     * The search is done ignore the character case.
     * @param source the string to search
     * @param str the string to search for
     * @return the index of the first occurrence of the string in the
     *          character sequence represented by this object, or
     *          <code>-1</code> if the string does not occur.
     * @ibm-api
     */
    public static int lastIndexOfIgnoreCase(String source, String str) {
        return lastIndexOfIgnoreCase(source, str, source.length());
    }
}

Related

  1. lastIndexOfAny(String str, String search, int offset)
  2. lastIndexOfAnyBut(String srcString, String validString)
  3. lastIndexOfAnyDelimiter(String str, int fromIndex, String delimiters)
  4. lastIndexOfIgnoreCase(String pString, String pLookFor)
  5. lastIndexOfIgnoreCase(String s, String substr)
  6. lastIndexOfIgnoreCase(String str, String searchStr)
  7. lastIndexOfIgnoreCase(String str, String searchStr)
  8. lastIndexOfIgnoreCase(String str, String searchStr, int startPos)
  9. lastIndexOfImpl(final String str, final String search, final int fromIndex, final boolean ignoreCase)