Java examples for java.lang:String Index
Finds first 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(indexOf(val, c)); }//from w w w .ja v a2 s . c o m /** * Finds first 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 indexOf(CharSequence val, char c, int start) { int res = -1; for (int i = start; val.length() > i; ++i) { if (val.charAt(i) == c) { res = i; break; } } return res; } /** * Finds first 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 indexOf(CharSequence val, char c) { return indexOf(val, c, 0); } }