Java StringBuilder substring() Method

Java StringBuilder substring() Method | The substring is a part of the string if the string is “Pen is mightier than the sword” then the words “than the” would be substring. In the Java StringBuilder class, we have a method called substring() which returns the substring from the given index and extends till the end of the string or character sequence.

There are two variations of the substring() method in the StringBuilder class. The syntax for the substring methods are as follows:-

  1. public String substring(int start)
  2. public String substring(int start, int end)

Parameters:-
start: indicates from which index to start, inclusive.
end: The ending index, exclusive.
Return type: String
Returns: the substring from the specified index.
Exception:-

  • The public String substring(int start) method throws StringIndexOutOfBoundsException if the parameter start is less than 0 or more than the length of the string.
  • The public String substring(int start, int end) method throws StringIndexOutOfBoundsException if the start or end is negative or greater than length() or the start is greater than the end.

Java StringBuilder substring() Method Example

Let us see an example of the public String substring(int start) method. The start parameter is inclusive.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Hello World! Java Programming Langauge");
      System.out.println("String = " + string);
      System.out.println("SubSequence = " + string.substring(13));
   }
}

Output:-

String = Hello World! Java Programming Langauge
SubSequence = Java Programming Langauge

Let us see an example of the public String substring(int start, int end) method. The start parameter is inclusive, but the end parameter is exclusive.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Hello World! Java Programming Langauge");
      System.out.println("String = " + string);
      System.out.println("SubSequence = " + string.substring(13, 18));
   }
}

Output:-

String = Hello World! Java Programming Langauge
SubSequence = Java Programming Langauge

How To Get Substring From StringBuilder In Java

In the below program, the string is “All is well” from this string we have extracted the substring “is well” which starts from index 3.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("All is well");
      System.out.println("String = " + string);
      System.out.println("SubSequence = " + string.substring(4));
   }
}

Output:-

String = All is well
SubSequence = is well

How To See If StringBuilder Contains A Substring In Java

To check whether the substring is present in the given StringBuilder or not we can use contains() method of the string class. The contains() method will not work for the StringBuilder class hence we have to convert the StringBuilder to the string. Then we can use contains() method on the string object. This contains() method checks for the substring in the string, if present then it returns true else it returns false. The syntax for the contains() method is as follows:- public String contains(String substring);

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("All is well");
      System.out.println("StringBuilder  = " + sb);

      String string = sb.toString();
      System.out.println("StringBuilder contains \"is\"?: " + string.contains("is"));
   }
}

Output:-

StringBuilder = All is well
StringBuilder contains “is”?: true

Delete Substring StringBuilder Java

Let us see how to delete substring from StringBuilder in Java. The delete() method of the StringBuilder class removes the substring at the specified index. The syntax for the delete method is as follows:- public String delete(int start, int end)

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("All is well");
      System.out.println("StringBuilder before deleting the substring: " 
                         + string);
      System.out.println("StringBuilder after deleting the subtring: " 
                         + string.delete(3, 6));
   }
}

Output:-

StringBuilder before deleting the substring: All is well
StringBuilder after deleting the subtring: All well

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.