java.lang.StringBuffer substring(int start)

Description :

This java tutorial shows how to use the substring(int start) method of StringBuffer class under java.lang package.

This method returns a new String that contains a subsequence of characters currently contained in this character sequence. Make a note that the substring begins at the specified index and extends to the end of this character sequence. This method is similar with substring(int start, int end) its just that in this case the end is sb.length().

Method Syntax :

public String substring(int start)

Parameter Input :

DataType Parameter Description
int start the beginning index

Method Returns :

The substring(int start) method of StringBuffer class returns a string composing of the original character sequences chopped from the index specified as method argument until the end of the character sequence.

Compatibility Version :

Requires Java 1.2 and up

Java Code Example :

This java example source code demonstrates the use of substring(int start)  method of StringBuffer class. Initially the code assigns a string “” as initial contents of the string buffer. Then we use the substring method with index 5 as starting point in getting the character sequences of the buffer. Calling this method returns a new sets of characters which comes from the stringbuffer object and chopped from the starting index specified as method argument.


 * This example source code demonstrates the use of 
 * substring(int start) method of StringBuffer class

public class StringBufferSubstringStart {

	public static void main(String[] args) {

		// initialize the StringBuffer object
		StringBuffer sb = new StringBuffer("");
		System.out.println("Contents of buffer:" + sb);

		// get the string from buffer from index 5
		int start = 5;		
		String str = sb.substring(start);		


Sample Output :

Running the above example source code will give the following output

substring(int start) method example

substring(int start) method example

Exception Scenario :


The above exception will be if the following condition has been met:

  • if start is negative
  • start is greater than the StringBuffer length

Suggested Reading List :

References :