In this tutorial, you will get the detailed explanation about the indexOf() method of String class.
In this tutorial, you will get the detailed explanation about the indexOf() method of String class.In this tutorial we will discuss about indexOf() method in java. indexOf() method which is of string class, used to locate a character or string. The String class provide two method for searching a specified character or string, indexOf() and lastIndex(). For example you want to search a character 'a' in the String "abc" then the syntax is like as follows:
String str="abc"; int index=str.indexOf('a');
Then the index returns integer value 0 i.e. location of 'a' in the string abc.
The indexOf() method can be overloaded in different ways they are as follows:
Syntax :
public int indexOf(int ch)
Code of the program given below :
public class IndexOf { public static void main(String args[]) { String str="Hello java world"; int index=str.indexOf('e'); String substr="java"; System.out.println(""); System.out.println("Index of e is = "+index); index=str.indexOf(str, 2); System.out.println(""); System.out.println("Index of o is at "+index); System.out.println(""); System.out.println("found at "+str.indexOf(substr)); System.out.println(""); System.out.println("found at "+str.indexOf(substr,7)); } }
Output from the program :
Ads