Java String Last Index Of lastIndexOfImpl(final String str, final String search, final int fromIndex, final boolean ignoreCase)

Here you can find the source of lastIndexOfImpl(final String str, final String search, final int fromIndex, final boolean ignoreCase)

Description

Searchs the index of the last occurrence of a substring in a string.

License

Apache License

Parameter

Parameter Description
str the source string, which can't be null nor empty.
search the substring to be seearched, which can't be null nor empty.
fromIndex the index of the source string where to start searching backward.
ignoreCase whether to ignore case while comparing strings.

Return

the index of the last occurrence of a substring in the source string.

Declaration

private static int lastIndexOfImpl(final String str, final String search, final int fromIndex,
        final boolean ignoreCase) 

Method Source Code

//package com.java2s;
/*/*from  www.java2  s.  c o  m*/
 * Copyright (c) 2014  Haixing Hu
 *
 * 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 {
    /**
     * Searchs the index of the last occurrence of a substring in a string.
     *
     * @param str
     *          the source string, which can't be null nor empty.
     * @param search
     *          the substring to be seearched, which can't be null nor empty.
     * @param fromIndex
     *          the index of the source string where to start searching backward.
     * @param ignoreCase
     *          whether to ignore case while comparing strings.
     * @return the index of the last occurrence of a substring in the source
     *         string.
     */
    private static int lastIndexOfImpl(final String str, final String search, final int fromIndex,
            final boolean ignoreCase) {
        // assume str != null && search != null && str.length() > 0 &&
        // search.length() > 0 && fromIndex >= 0 && fromIndex < str.length()
        final int searchLen = search.length();
        for (int i = fromIndex; i >= 0; --i) {
            if (str.regionMatches(ignoreCase, i, search, 0, searchLen)) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. lastIndexOfIgnoreCase(String s, String substr)
  2. lastIndexOfIgnoreCase(String source, String str, int fromIndex)
  3. lastIndexOfIgnoreCase(String str, String searchStr)
  4. lastIndexOfIgnoreCase(String str, String searchStr)
  5. lastIndexOfIgnoreCase(String str, String searchStr, int startPos)
  6. lastIndexOfLetter(String string)
  7. lastIndexOfNewline(CharSequence theChars, int aStart)
  8. lastIndexOfNonWhitespace(final String src)
  9. lastIndexOfOrdinal(String value, String part, int startIndex, int count, boolean caseSensitive)