Here you can find the source of lastIndexOf(String s, String sub, int startIndex, int endIndex)
Parameter | Description |
---|---|
s | source to examine |
sub | substring to find |
startIndex | starting index |
endIndex | end index |
public static int lastIndexOf(String s, String sub, int startIndex, int endIndex)
//package com.java2s; public class Main { /**/*w w w .ja va 2 s . c o m*/ * Finds last index of a substring in the given source string in specified range [end, start] * See {@link #indexOf(String, String, int, int)} for details about the speed. * * @param s source to examine * @param sub substring to find * @param startIndex starting index * @param endIndex end index * @return last index of founded substring or -1 if substring is not found */ public static int lastIndexOf(String s, String sub, int startIndex, int endIndex) { int sublen = sub.length(); int srclen = s.length(); if (sublen == 0) { return startIndex > srclen ? srclen : (startIndex < -1 ? -1 : startIndex); } int total = srclen - sublen; if (total < 0) { return -1; } if (startIndex >= total) { startIndex = total; } if (endIndex < 0) { endIndex = 0; } char c = sub.charAt(0); mainloop: for (int i = startIndex; i >= endIndex; i--) { if (s.charAt(i) != c) { continue; } int j = 1; int k = i + 1; while (j < sublen) { if (sub.charAt(j) != s.charAt(k)) { continue mainloop; } j++; k++; } return i; } return -1; } /** * Finds last index of a character in the given source string in specified range [end, start] */ public static int lastIndexOf(String s, char c, int startIndex, int endIndex) { int total = s.length() - 1; if (total < 0) { return -1; } if (startIndex >= total) { startIndex = total; } if (endIndex < 0) { endIndex = 0; } for (int i = startIndex; i >= endIndex; i--) { if (s.charAt(i) == c) { return i; } } return -1; } /** * Finds the very last index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array */ public static int[] lastIndexOf(String s, String arr[]) { return lastIndexOf(s, arr, s.length()); } /** * Finds the very last index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array * @param fromIndex starting position */ public static int[] lastIndexOf(String s, String arr[], int fromIndex) { int arrLen = arr.length; int index = -1; int last = -1; for (int j = 0; j < arrLen; j++) { int i = s.lastIndexOf(arr[j], fromIndex); if (i != -1) { if (i > index) { index = i; last = j; } } } return last == -1 ? null : new int[] { last, index }; } /** * Finds the very last index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param c char array */ public static int[] lastIndexOf(String s, char c[]) { return lastIndexOf(s, c, s.length()); } /** * Finds the very last index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param c char array * @param fromIndex starting position */ public static int[] lastIndexOf(String s, char c[], int fromIndex) { int arrLen = c.length; int index = -1; int last = -1; for (int j = 0; j < arrLen; j++) { int i = s.lastIndexOf(c[j], fromIndex); if (i != -1) { if (i > index) { index = i; last = j; } } } return last == -1 ? null : new int[] { last, index }; } }