Site icon TechVidvan

Java String indexOf() Method with Examples

java string indexOf()

Java is a simple programming language that may be used in a variety of applications. The String indexof() function is commonly used in Java. It mainly used in get substring in String and in index of character.

Explanation

The indexOf() function returns the location in a string of the first occurrence of the provided character(s).

The lastIndexOf function returns the position of the final occurrence of a string’s supplied character(s). 

There are 4 indexOf() methods in Java:

Parameter:

Technical Details:

Return value:

Behaviour:

Method Signature:

int indexOf(String str)
int indexOf(String str, int fromIndex)

Four methods Signature of IndexOf():

int indexOf(int ch) For the specified char value, it returns the index position.
int indexOf(int ch, int fromIndex) It returns the index position from index for the specified char value.
int indexOf(String substring) For the specified substring, it returns the index position.
int indexOf(String substring, int fromIndex) For the specified substring, it returns the index position, and from index

Internal of Indexof():

public int indexOf(int ch) {  
        return indexOf(ch, 0);  
    }

Example 1:

public class TechVidvan IndexOf{  
public static void main(String args[]){  
String s1="Indexof method is used ";  
  
int index1=s1.indexOf("is");

int index2=s1.indexOf("index");

System.out.println(index1+"  "+index2);
  
int index3 = s1.indexOf("is", 4);
        System.out.println(index3);

        int index4 = s1.indexOf('s');
        System.out.println(index4);
    }
}

Output:
8 0
9
3

Example 2:

public class IndexOf {  
    public static void main(String[] args) {  
        String s1 = "Java is easy to learn";             
        int index = s1.indexOf("easy");         System.out.println("The index of substring "+index);          
    }  
  
}

Output:
The index of Substring 8

Example 3:

public class TechVidvanIndexOf {  
    public static void main(String[] args) {      
        String s1 = "This is indexOf method";         
          
        int index = s1.indexOf("method", 10);  
        System.out.println("index of substring "+index);  
        index = s1.indexOf("method", 20);  
        System.out.println("index of substring "+index);          
    }  
}

Output:
index of substring 16
index of substring -1

Table:

IndexOf(Char, Int32) This function returns the zero-based index of the first instance of the provided Unicode character in this string. The search begins at a given character point.
IndexOf(String) The zero-based index of the first occurrence of the provided string in this instance is reported.
IndexOf(Char) This function returns the zero-based index of the first instance of the provided Unicode character in this string.
IndexOf(String, Int32) The zero-based index of the first occurrence of the provided string in this instance is reported. The search begins at a given character point.

Variants of indexof():

1. int indexOf():

This function returns the index of the first occurrence of the provided character within this string, or -1 if the character does not occur.

Syntax:

int indexOf(char ch )

Parameters:

ch : character.

Example:

public class IndexOf{
public static void main(String[] args) {		
        String s1 = "We use Strings";  		
        // Passing char and index from
        int index = s1.indexOf('e', 5); //Returns the index of this char
        System.out.println("index of char "+index);		
    }
}

Output:
index of char 5

2. int indexOf(char ch, int strt)

This function returns the index of the first occurrence of the supplied character within this string, commencing the search at the specified index, or -1 if the character does not occur.

Syntax:

int indexOf(char ch, int strt)

Parameters:

ch :a character.
str : the index to start the search from.

3. int indexOf(String str)

This function returns the index of the first occurrence of the provided substring within this string. If it does not appear as a substring, it returns -1.

Syntax:

int indexOf(String str)

Parameters:

str : a string.

4. int indexOf(String str, int strt)

This function returns the index of the first occurrence of the provided substring within this text. If it does not appear as a substring, it returns -1.

Syntax:

int indexOf(String str, int str)

Parameters:

str : a string.

Usage of string indexof():

Exceptions of Java String indexof():

Advantages of String indexof() in Java:

Disadvantages of String indexof() in Java:

Conclusion

In Java, we learned about the String indexof() method function. It is primarily useful in the basic substring. This type of String function is used in many areas. We have learned all the content of String indexof().

Exit mobile version