Java String indexOf()

Java String indexOf() Method

In this tutorial, we will learn about indexOf() method in Java for strings. We’ll learn about its syntax and see some programming examples to find characters or substrings in a Java string.

One common use case is when a system admin needs to find the index of the ‘@’ character in an email ID and extract the remaining substring. In such situations, the indexOf() method proves useful.

The indexOf() method in Java String allows us to obtain the index of the first occurrence of a specific substring within a string.

Syntax :

There are some following Syntaxes available in Java String indexof-

  •   public int indexOf(String str)
  •   public int indexOf(String str, int fromIndex)
  •   public int indexOf(int char)
  •   public int indexOf(int char, int fromIndex

Parameters :

The indexOf method in Java for strings has the following parameters:

  • char: Represents a single character to search for.
  • str: Represents the string to be searched.
  • fromIndex: Represents the starting index position for the search.

indexOf(char b): This method returns the index of the character ‘b’ in the string. If the character is not found, it returns -1.

indexOf(char c, int startIndex): This method returns the index of the first occurrence of character ‘c’ after the specified startIndex. Occurrences before startIndex are ignored.

indexOf(String substring): This method returns the index of the first character of the specified substring within the string. If the substring is not found, it returns -1.

indexOf(String substring, int startIndex): This method returns the index of the first character of the specified substring after the specified startIndex. If the substring starts at startIndex, it is ignored.

Java indexOf() Examples

1. In this example, we will give a character. This method will return the position of that character in the given string.

public class IndexOfExample{
  public static void main(String []args){
    String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
    System.out.println("Index of given character is:"+fullStr.indexOf('L'));
  }
}


Output :

Index of given character is:4

2. In Java String indexOf() example, we will give a character and start from the value. This method will return the position of that character in the given string.

public class IndexOfExample{
  public static void main(String []args){
  String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
  System.out.println("Index of given character is:"+fullStr.indexOf('L',6));
  }
}


Output :

Index of given character is: 12

3. In this example, we will give a String. This method will return the position of that String in the given String.

public class IndexOfExample{
  public static void main(String []args){
   String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
   System.out.println("Index of given character is:"+fullStr.indexOf("LO"));
  }
}


Output :

Index of given character is:4

4. In this example, we will give a String and start from a value. This method will return the position of that String in the given String.

public class IndexOfExample{
 public static void main(String []args){
 String fullStr = "DEVELOPER HELPS IS A PRORAMMING WEBSITE";
 System.out.println("Index of given character is:"+fullStr.indexOf("EL",5));
 }
}


Output :

Index of given character is:11

5. Let us consider a programming code of string.indexof java

public class UniqueIndexOf {
    public static void main(String[] args) {
        String input = "Hello, World!";
        char target = 'W';
        int index = customIndexOf(input, target);
        System.out.println("Index: " + index);
    }
    
    public static int customIndexOf(String str, char target) {
        int length = str.length();
        
        for (int i = 0; i < length; i++) {
            if (str.charAt(i) == target) {
                return i;
            }
        }
        
        return -1; // Target character not found
    }
}


Output :

Index : 7

Java indexOf() return value

returns the index of the first occurrence of the specified character/string

returns -1 if the specified character/string is not found.

More Related Post

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

https://www.facebook.com/developerhelps/

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!