Java examples for java.lang:String Index
Search for last Index Of a character in a String
//package com.java2s; public class Main { public static void main(String[] argv) { String string = "java2s.com"; char value = 'o'; int startIndex = 2; int count = 4; System.out.println(lastIndexOf(string, value, startIndex, count)); }//from w w w .j av a2 s .c o m public static int lastIndexOf(String string, char value, int startIndex, int count) { int leftMost = startIndex + 1 - count; int rightMost = startIndex + 1; String substring = string.substring(leftMost, rightMost); int lastIndexInSubstring = substring.lastIndexOf(value); if (lastIndexInSubstring < 0) return -1; else return lastIndexInSubstring + leftMost; } public static int lastIndexOf(String string, String value, int startIndex, int count) { int leftMost = startIndex + 1 - count; int rightMost = startIndex + 1; String substring = string.substring(leftMost, rightMost); int lastIndexInSubstring = substring.lastIndexOf(value); if (lastIndexInSubstring < 0) return -1; else return lastIndexInSubstring + leftMost; } }