- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuffer substring() Method
The Java StringBuffer substring() method is, used to retrieve a desired subsequence from a StringBuffer object. The substring is a small part of the given string. By default, This substring begins at the specified index and extends to the end of this sequence.
The substring() method accepts a parameter as an integer that holds the value of the start index. It throws an exception if the start index value is negative or greater than the sequence length.
The substring() method has two polymorphic variants with different parameters, as shown in the syntax below.
Syntax
Following is the syntax of the Java StringBuffer substring() method −
public String substring(int start) public String substring(int start, int end)
Parameters
- start − This is the beginning index, inclusive.
- end − This is the ending index, exclusive.
Return Value
This method returns the new string from start index till end of sequence.
If you pass the end value as an argument, this method returns the new string from start index till end index.
Example
If the given start index value is positive and less than the given sequence length, the substring() method returns a new substring.
In the following program, we are instantiating the StringBuffer class with the value of “Java Language”. Then, using the substring() method, we are, trying to retrieve the new sub-string from the given sequence at the specified start index 6.
public class SubString { public static void main(String[] args) { //instantiate of the StringBuffer class StringBuffer sb = new StringBuffer("Java Language"); System.out.println("The given string: " + sb); //initialize the start index value int startIndex = 5; System.out.println("The given start index value is: " + startIndex); //using the substring() method System.out.println("The new sub-string is: " + sb.substring(startIndex)); } }
Output
On executing the above program, it will produce the following result −
The given string: Java Language The given start index value is: 5 The new sub-string is: Language
Example
If the given start index value is greater than the sequence length, the substring() method throws the StringIndexOutOfBoundsException.
In the following example, we are creating an object of the StringBuffer class with the value of “Tutorix”. Using the substring() method, we are trying to retrieve the substring from the given sequence at the specified start index 10, whose value is greater than the sequence length.
public class SubString { public static void main(String[] args) { try { //create an object of this class StringBuffer sb = new StringBuffer("Tutorix"); System.out.println("The given string: " + sb); //initialize the start index value int startIndex = 10; System.out.println("The given start index value is: " + startIndex); //using the substring() method System.out.println("The new sub-string is: " + sb.substring(startIndex)); } catch(Exception e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }
Output
Following is the output of the above program −
The given string: Tutorix The given start index value is: 10 java.lang.StringIndexOutOfBoundsException: Range [10, 7) out of bounds for length 7 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112) at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349) at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1057) at java.base/java.lang.StringBuffer.substring(StringBuffer.java:525) at java.base/java.lang.StringBuffer.substring(StringBuffer.java:507) at SubString.main(SubString.java:13) Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 7) out of bounds for length 7
Example
If the given start index value is negative, this method throws the StringIndexOutOfBoundsException.
In this program, we are instantiating the StringBuffer class with the value of “JavaProgramming”. Then, using the substring() method, we are, trying to retrieve the substring of the given sequence at the specified start index -1.
public class SubString { public static void main(String[] args) { try { //instantiate of the StringBuffer class StringBuffer sb = new StringBuffer("JavaProgramming"); System.out.println("The given string: " + sb); //initialize the start index value int startIndex = -1; System.out.println("The given start index value is: " + startIndex); //using the substring() method System.out.println("The new sub-string is: " + sb.substring(startIndex)); } catch(StringIndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } }
Output
The above program, produces the following results −
The given string: JavaProgramming The given start index value is: -1 java.lang.StringIndexOutOfBoundsException: Range [-1, 15) out of bounds for length 15 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112) at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349) at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1057) at java.base/java.lang.StringBuffer.substring(StringBuffer.java:525) at java.base/java.lang.StringBuffer.substring(StringBuffer.java:507) at SubString.main(SubString.java:13) Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, 15) out of bounds for length 15
Example
If the given start index and end index value are positive, and less than the sequence length, the substring() method returns the new substring.
In this program, we are creating an object of the StringBuffer class with the value of “Java Programming Language”. Then, using the substring() method, we are, trying to retrieve the substring of the given sequence at the specified start index 5, and end index 16.
public class SubString { public static void main(String[] args) { //instantiate of the StringBuffer class StringBuffer sb = new StringBuffer("Java Programming Language"); System.out.println("The given string: " + sb); //initialize the start index and end index values int startIndex = 5; int endIndex = 16; System.out.println("The given start index and end index values are: " + startIndex + " and " + endIndex); //using the substring() method System.out.println("The new sub-string is: " + sb.substring(startIndex, endIndex)); } }
Output
After executing the above program, it will generate the following output −
The given string: Java Programming Language The given start index and end index values are: 5 and 16 The new sub-string is: Programming