Java examples for java.lang:String Index
Finds last occurrence of character in CharSequence starting from index start.
//package com.java2s; public class Main { public static void main(String[] argv) { CharSequence val = "java2s.com"; char c = 'a'; System.out.println(lastIndexOf(val, c)); }//from w w w. j av a 2 s .c o m /** * Finds last occurrence of character in CharSequence starting from index start. * * @param val Sequence to search for character * @param c Character to search for * @param start Starting index in sequence to search * @return Index in sequence where character is found or -1 * if it wasn't found */ public static int lastIndexOf(CharSequence val, char c, int start) { int res = -1; int i = start; if (val.length() <= i) i = val.length() - 1; for (; 0 <= i; --i) { if (val.charAt(i) == c) { res = i; break; } } return res; } /** * Finds last occurrence of character in CharSequence starting from the beginning. * * @param val Sequence to search for character * @param c Character to search for * @return Index in sequence where character is found or -1 * if it wasn't found */ public static int lastIndexOf(CharSequence val, char c) { return lastIndexOf(val, c, val.length() - 1); } }