StringBuffer substring() method in Java

In this tutorial, we will learn about StringBuffer substring() method in Java.

Sometimes we might need to get substring out of a String in java. StringBuffer class in Java provides substring() method to get substring from a String. There are following two variants of substring() method in StringBuffer class.

This method returns a new String that contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified index and extends to the end of this sequence.

Parameters:
start – The beginning index, inclusive.

Returns:
The new string.

Throws:
StringIndexOutOfBoundsException – if start is less than zero, or greater than the length of this object.

This method returns a new String that contains a subsequence of characters currently contained in this sequence. The substring begins at the specified start and extends to the character at index end – 1.

Parameters:
start – The beginning index, inclusive.
end – The ending index, exclusive.

Returns:
The new string.

Throws:
StringIndexOutOfBoundsException – if start or end are negative or greater than length(), or start is greater than end.

Example:

Output:

Related posts

Leave a Comment