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

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

Description

Finds the last index in the CharSequence that matches the specified character.

License

Open Source License

Parameter

Parameter Description
cs the CharSequence to be processed
searchChar the char to be searched for
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

Declaration

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

Method Source Code

//package com.java2s;

public class Main {
    /**/*  w  w  w  .  jav  a 2s .co m*/
     * <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(CharSequence cs, int searchChar, int start) {
        if (cs instanceof String) {
            return ((String) cs).lastIndexOf(searchChar, start);
        } else {
            int sz = cs.length();
            if (start < 0) {
                return -1;
            }
            if (start >= sz) {
                start = sz - 1;
            }
            for (int i = start; i >= 0; --i) {
                if (cs.charAt(i) == searchChar) {
                    return i;
                }
            }
            return -1;
        }
    }

    /**
     * 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(CharSequence cs, CharSequence searchChar, int start) {
        return cs.toString().lastIndexOf(searchChar.toString(), start);
        //        if (cs instanceof String && searchChar instanceof String) {
        //            // TODO: Do we assume searchChar is usually relatively small;
        //            //       If so then calling toString() on it is better than reverting to
        //            //       the green implementation in the else block
        //            return ((String) cs).lastIndexOf((String) searchChar, start);
        //        } else {
        //            // TODO: Implement rather than convert to String
        //            return cs.toString().lastIndexOf(searchChar.toString(), start);
        //        }
    }
}

Related

  1. lastIndexOf(char ch, String str)
  2. lastIndexof(char chr, int pos, CharSequence str)
  3. lastIndexOf(CharSequence chars, String searched)
  4. lastIndexOf(CharSequence charSeq, char ch)
  5. lastIndexOf(CharSequence haystack, char needle)
  6. lastIndexOf(CharSequence s, char c, int start, int end)
  7. lastIndexOf(CharSequence s, CharSequence seq)
  8. lastIndexOf(CharSequence theChars, CharSequence theSearch)