Java examples for java.lang:String Index
Search String for last Index Of Any character in it
public class Main{ public static void main(String[] argv){ String string = "java2s.com"; char[] anyOf = new char[]{'b','o','o','k','2','s','.','c','o','m','','1',}; System.out.println(lastIndexOfAny(string,anyOf)); }//from w ww. j a v a 2 s . co m public static int lastIndexOfAny(String string, char[] anyOf) { int highestIndex = -1; for (char c : anyOf) { int index = string.lastIndexOf(c); if (index > highestIndex) { highestIndex = index; if (index == string.length() - 1) break; } } return highestIndex; } public static int lastIndexOfAny(String string, char[] anyOf, int startIndex) { String substring = string.substring(0, startIndex + 1); int lastIndexInSubstring = lastIndexOfAny(substring, anyOf); if (lastIndexInSubstring < 0) return -1; else return lastIndexInSubstring; } public static int lastIndexOfAny(String string, char[] anyOf, int startIndex, int count) { int leftMost = startIndex + 1 - count; int rightMost = startIndex + 1; String substring = string.substring(leftMost, rightMost); int lastIndexInSubstring = lastIndexOfAny(substring, anyOf); if (lastIndexInSubstring < 0) return -1; else return lastIndexInSubstring + leftMost; } }