public int lastIndexOf ( CharSequence target , int fromIndex , int endIndex ) { if ( fromIndex < 0 ) throw new IndexOutOfBoundsException ( "index out of range: " + fromIndex ) ; if ( endIndex < 0 ) throw new IndexOutOfBoundsException ( "index out of range: " + endIndex ) ; if ( fromIndex > length ( ) ) throw new IndexOutOfBoundsException ( "fromIndex > length()" ) ; if ( fromIndex > endIndex ) throw new IndexOutOfBoundsException ( "fromIndex > endIndex" ) ; if ( endIndex >= builder . length ( ) ) { endIndex = builder . length ( ) - 1 ; } int targetCount = target . length ( ) ; if ( targetCount == 0 ) { return endIndex ; } int rightIndex = endIndex - targetCount ; if ( fromIndex > rightIndex ) { fromIndex = rightIndex ; } int strLastIndex = targetCount - 1 ; char strLastChar = target . charAt ( strLastIndex ) ; int min = fromIndex + targetCount - 1 ; lastCharSearch : for ( int i = endIndex ; i >= min ; i -- ) { if ( builder . charAt ( i ) == strLastChar ) { for ( int j = strLastIndex - 1 , k = 1 ; j >= 0 ; j -- , k ++ ) { if ( builder . charAt ( i - k ) != target . charAt ( j ) ) { continue lastCharSearch ; } } return i - target . length ( ) + 1 ; } } return - 1 ; }
Returns the index within this string of the last occurrence of the specified substring between the specified indices .