Java StringBuilder indexOf() Method

Java StringBuilder indexOf() Method | In this blog, we will learn about the indexOf() method in the StringBuilder class. This method is used to return the first occurrence of the passed substring. If the given substring is not present in the string then the indexOf() method returns -1.

The indexOf() method has two variations:-
1. public int indexOf(String substring)
2. public int indexOf(Strign substring, int start)
Parameters:
substring- the sub-string needed to be searched in the string.
Start:- from which index substring needed to be searched.
Return type:- integer.
Returns: the index of the substring.

An example of the indexOf() method is as follows:-
Exampe1:
String:- “Hello People”
Substring:- “ll”
Output:- 2

Example2:
String: “Names”
Substring:- “ames”
Output: 1

StringBuilder indexOf() Java Example-1

Let us see some examples of the StringBuilder class indexOf() method. We will create a StringBuilder object by taking some strings and checking for the index of some substring.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Know Program");
      System.out.println("StringBuilder = " + string);
      int index = string.indexOf("Pro");
      System.out.println("Index = " + index);
   }
}

Output:-

StringBuilder = Know Program
Index = 5

Java StringBuilder indexOf() Method Example-2

In the below program we search for the substring which is not present in the given string. In that case Java StringBuilder indexOf() Method returns -1.

public class Main {
   public static void main(String[] args) {
      String string = "Creditors have a better memory than debtors";
      StringBuilder sb = new StringBuilder(string);
      System.out.println("StringBuilder = " + sb);

      int index = sb.indexOf("Hi");
      System.out.println("Index = " + index);
   }
}

Output:-

StringBuilder = Creditors have a better memory than debtors
Index = -1

IndexOf() StringBuilder Java Example-3

In the below program the substring “to” has been repeated twice, as we are now using the other variation of the indexOf() method we are retrieving the second occurrence of the substring.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Early to bed, early to rise");
      System.out.println("StringBuilder = " + string);

      int index = string.indexOf("to");
      System.out.println("Index = " + index);

      int index1 = string.indexOf("to", 10);
      System.out.println("Index = " + index1);
   }
}

Output:-

StringBuilder = Early to bed, early to rise
Index = 6
Index = 20

The indexOf(“to”) method will search for the substring “to” from the beginning of the StringBuilder object, whereas indexOf(“to”, 10) will start search substring “to” from the 10th index of the StringBuilder object.

Java StringBuilder indexOf Example-4

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Practice makes the man perfect");
      System.out.println("StringBuilder = " + string);
      int ind = string.indexOf("ma", 15);
      System.out.println("Index = " + ind);
   }
}

Output:

StringBuilder = Practice makes the man perfect
Index = 19

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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