Find the position or index of substring or character in a String Java
indexof(char a), indexOf(char a,int startfrom), indexOf(String a) and indexOf(String a,int startfrom) methods of String class can be used to find the first occurrence of the sub-string in a String.
lastIndexof(char a), lastIndexOf(char a,int startfrom), lastIndexOf(String a) and lastIndexOf(String 1,int startfrom) methods of String class can be used to find the last occurrence of the sub-string in a String.
The above methods returns the position if a substring is occurred in the given string and returns -1 if substring is not in the given string.
Example 1 ( indexOf(char a) )
indexOf(char a) is used to find the position of first occurrence of the character in the given String.
class StringTest{
public static void main(String args[]){
String str="Nice";
char a='c';
int position=str.indexOf(a);
System.out.println("Position of '"+a+"' in the String is "+position);
}
}
Output 1
Position of 'c' in the String is 2
Example 2 ( indexOf(char a,int startFrom) )
indexOf(char a,int startFrom) is used to find the position of first occurrence of the character after the startFrom position in the given String.
class StringTest{
public static void main(String args[]){
String str="Wonderful Wonderful";
char a='o';
int position=str.indexOf(a,3);
System.out.println("Position of '"+a+"' in the String is "+position);
}
}
Output 2
Position of 'o' in the String is 11
Example 3 ( indexOf(String a) )
indexOf(String a) is used to find the position of first occurrence of the substring in the given String.
class StringTest{
public static void main(String args[]){
String str="You are nice";
String substr="nice";
int position=str.indexOf(substr);
System.out.println("Position of '"+substr+"' in the String is "+position);
}
}
Output 3
Position of 'nice' in the String is 8
Example 4 ( indexOf(String a,int startFrom) )
indexOf(String a,int startFrom) is used to find the position of first occurrence of the substring after the startFrom position in the given String.
class StringTest{
public static void main(String args[]){
String str="My friend and your friend";
String substr="friend";
int startFrom=5;
int position=str.indexOf(substr,startFrom);
System.out.println("Position of '"+substr+"' in the String is "+position);
}
}
Output 4
Position of 'friend' in the String is 19
Example 5 ( lastIndexOf(char a) )
lastIndexOf(char a) is used to find the position of last occurrence of the character in the given String.
class StringTest{
public static void main(String args[]){
String str="Character";
char a='r';
int position=str.lastIndexOf(a);
System.out.println("Position of '"+a+"' in the String is "+position);
}
}
Output 5
Position of 'r' in the String is 8
Example 6 ( lastIndexOf(char a,int startFrom) )
lastIndexOf(char a,int startFrom) is used to find the position of last occurrence of the character searching backwards from startFrom position in the String.
class StringTest{
public static void main(String args[]){
String str="Character is superb";
char a='r';
int startFrom=5;
int position=str.lastIndexOf(a,startFrom);
System.out.println("Position of '"+a+"' in the String is "+position);
}
}
Output 6
Position of 'r' in the String is 3
Example 7 ( lastIndexOf(String a) )
lastIndexOf(String a) is used to find the position of last occurrence of the substring in the given String.
class StringTest{
public static void main(String args[]){
String str="Mm Mm";
String substr="Mm";
int position=str.lastIndexOf(substr);
System.out.println("Position of '"+substr+"' in the String is "+position);
}
}
Output 7
Position of 'Mm' in the String is 3
Example 8 ( lastIndexOf(String a,int startFrom) )
lastIndexOf(String a,int startFrom) is used to find the position of last occurrence of the substring searching backwards from startFrom position in the given String.
class StringTest{
public static void main(String args[]){
String str="Friend indeed is a friend in need";
String substr="in";
int startFrom=5;
int position=str.lastIndexOf(substr,startFrom);
System.out.println("Position of '"+substr+"' in the String is "+position);
}
}
Output 8
Position of 'in' in the String is -1