Java String Last Index Of lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start)

Here you can find the source of lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start)

Description

Used by the lastIndexOf(CharSequence methods) as a green implementation of lastIndexOf

License

Apache License

Parameter

Parameter Description
cs the CharSequence to be processed
searchChar the CharSequence to be searched for
start the start index

Return

the index where the search sequence was found

Declaration

static int lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start) 

Method Source Code

//package com.java2s;
/*//from  w ww.  ja  va 2 s .  c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 {
    private static final int NOT_FOUND = -1;

    /**
     * <p>Finds the last index in the {@code CharSequence} that matches the
     * specified character.</p>
     *
     * @param cs  the {@code CharSequence} to be processed
     * @param searchChar  the char to be searched for
     * @param start  the start index, negative returns -1, beyond length starts at end
     * @return the index where the search char was found, -1 if not found
     */
    static int lastIndexOf(final CharSequence cs, final int searchChar, int start) {
        if (cs instanceof String) {
            return ((String) cs).lastIndexOf(searchChar, start);
        }
        final int sz = cs.length();
        if (start < 0) {
            return NOT_FOUND;
        }
        if (start >= sz) {
            start = sz - 1;
        }
        for (int i = start; i >= 0; --i) {
            if (cs.charAt(i) == searchChar) {
                return i;
            }
        }
        return NOT_FOUND;
    }

    /**
     * Used by the lastIndexOf(CharSequence methods) as a green implementation of lastIndexOf
     *
     * @param cs the {@code CharSequence} to be processed
     * @param searchChar the {@code CharSequence} to be searched for
     * @param start the start index
     * @return the index where the search sequence was found
     */
    static int lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start) {
        return cs.toString().lastIndexOf(searchChar.toString(), start);
    }
}

Related

  1. lastIndexOf(CharSequence cs, int searchChar, int start)
  2. lastIndexOf(CharSequence haystack, char needle)
  3. lastIndexOf(CharSequence s, char c, int start, int end)
  4. lastIndexOf(CharSequence s, CharSequence seq)
  5. lastIndexOf(CharSequence theChars, CharSequence theSearch)
  6. lastIndexOf(final CharSequence cs, final int searchChar, int start)
  7. lastIndexOf(final String input, final char delim)
  8. lastIndexOf(final String src, final String sub, int startIndex, int endIndex)
  9. lastIndexOf(String baseString, String indexString)