Here you can find the source of lastIndexOf(CharSequence cs, int searchChar, int start)
Finds the last index in the CharSequence that matches the specified character.
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 |
static int lastIndexOf(CharSequence cs, int searchChar, int start)
//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); // } } }