Java program to get the position of a character…


This java code will give you the index of a character in a given string.

class indexOfDemo
{
public static void main(String args [])
{
String s=”Hello Java Lerners.This is Demo of Searching of Strings.”;

System.out.println(s);

System.out.println(“index of ‘h’ : ” + s.indexOf(‘h’));
System.out.println(“index of “Java” : ” + s.indexOf(“Java”));

System.out.println(“Last index of ‘h’ : ” + s.lastIndexOf(‘h’));
System.out.println(“Last index of “of” : ” + s.indexOf(“of”));

}
}

Add Comment