We have seen how to get a particular character from a string by providing the index, the same manner if we want to know the index of a character [or position] with in a string, java provides a method .indexOf(char c), let’s see the implementation
public static void main(String[] args) { String myStr = new String("Welcome to qavalidation.com"); System.out.println("Index position of [t] - " + myStr.indexOf('t')); }
output
Index position of [t] - 8
In the above example, t occures multiple times in the string, charAt(‘t’) returns the index of 1st occurrence of character with in the string.