Java String Last Index Of lastIndexOfIgnoreCase(String str, String searchStr)

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

Description

Case in-sensitive find of the last index within a String.

A null String will return -1.

License

Apache License

Parameter

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

Return

the first index of the search String, -1 if no match or null string input

Declaration

public static int lastIndexOfIgnoreCase(String str, String searchStr) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*  ww w  .  j  a  v a 2s  .c  om*/
     * <p>Case in-sensitive find of the last index within a String.</p>
     * <p>
     * <p>A <code>null</code> String will return <code>-1</code>.
     * A negative start position returns <code>-1</code>.
     * An empty ("") search String always matches unless the start position is negative.
     * A start position greater than the string length searches the whole string.</p>
     * <p>
     * <pre>
     * BConfigStringUtil.lastIndexOfIgnoreCase(null, *)          = -1
     * BConfigStringUtil.lastIndexOfIgnoreCase(*, null)          = -1
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "A")  = 7
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "B")  = 5
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "AB") = 4
     * </pre>
     *
     * @param str       the String to check, may be null
     * @param searchStr the String to find, may be null
     *
     * @return the first index of the search String,
     * -1 if no match or <code>null</code> string input
     *
     * @since 2.5
     */
    public static int lastIndexOfIgnoreCase(String str, String searchStr) {
        if (str == null || searchStr == null) {
            return -1;
        }
        return lastIndexOfIgnoreCase(str, searchStr, str.length());
    }

    /**
     * <p>Case in-sensitive find of the last index within a String
     * from the specified position.</p>
     * <p>
     * <p>A <code>null</code> String will return <code>-1</code>.
     * A negative start position returns <code>-1</code>.
     * An empty ("") search String always matches unless the start position is negative.
     * A start position greater than the string length searches the whole string.</p>
     * <p>
     * <pre>
     * BConfigStringUtil.lastIndexOfIgnoreCase(null, *, *)          = -1
     * BConfigStringUtil.lastIndexOfIgnoreCase(*, null, *)          = -1
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "A", 8)  = 7
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", 8)  = 5
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", 9)  = 5
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", -1) = -1
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "A", 0)  = 0
     * BConfigStringUtil.lastIndexOfIgnoreCase("aabaabaa", "B", 0)  = -1
     * </pre>
     *
     * @param str       the String to check, may be null
     * @param searchStr the String to find, may be null
     * @param startPos  the start position
     *
     * @return the first index of the search String,
     * -1 if no match or <code>null</code> string input
     *
     * @since 2.5
     */
    public static int lastIndexOfIgnoreCase(String str, String searchStr, int startPos) {
        if (str == null || searchStr == null) {
            return -1;
        }
        if (startPos > (str.length() - searchStr.length())) {
            startPos = str.length() - searchStr.length();
        }
        if (startPos < 0) {
            return -1;
        }
        if (searchStr.length() == 0) {
            return startPos;
        }

        for (int i = startPos; i >= 0; i--) {
            if (str.regionMatches(true, i, searchStr, 0, searchStr.length())) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 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. lastIndexOfAnyBut(String srcString, String validString)
  2. lastIndexOfAnyDelimiter(String str, int fromIndex, String delimiters)
  3. lastIndexOfIgnoreCase(String pString, String pLookFor)
  4. lastIndexOfIgnoreCase(String s, String substr)
  5. lastIndexOfIgnoreCase(String source, String str, int fromIndex)
  6. lastIndexOfIgnoreCase(String str, String searchStr)
  7. lastIndexOfIgnoreCase(String str, String searchStr, int startPos)
  8. lastIndexOfImpl(final String str, final String search, final int fromIndex, final boolean ignoreCase)
  9. lastIndexOfLetter(String string)