string.indexOf() Returns the position of the matching string or char from left.
Syntax
string.indexOf(input,startPos)
input : Can be char or string. startPos : (optional ) Int position from where the matching will start
Note these points
Matching position is always returned from left, first position is 0 ( zero ).
First occurrence is returned ( from startPos index if given ) .
Search starts from 0 position if startPos is not given.
Using char input
String my_str="Welcome to plus2net.com";
System.out.println(my_str.indexOf("2"));// 15
System.out.println(my_str.indexOf("e"));// 1
Using char and startPos
String my_str="Welcome to plus2net.com";
System.out.println(my_str.indexOf("e",1));// 1
System.out.println(my_str.indexOf("e",2));// 6
Using string input
String my_str="Welcome to plus2net.com";
System.out.println(my_str.indexOf("co"));// 3
Using string and startPos
String my_str="Welcome to plus2net.com";
System.out.println(my_str.indexOf("co",3));// 3
System.out.println(my_str.indexOf("co",4));// 20
System.out.println(my_str.indexOf("com",4));// 20
Getting position of @ and . inside a email address.