It would be a mistake to judge the value of String against StringBuffer on a single point. Yes they are very similar in functionality when taking substrings. No this does not contribute to the argument as to whether to use one or the other.

You should always use the best structure. String is immutable and should be used when immutability is required and in some edge cases. If you need to make changes to a CharSequence then it would be better/safer/faster/more memory efficient to use StringBuilder.

Consider removing a part of a string. Compare how you can do it using a String and using a StringBuilder - see how much easier it is. It isn't just easier, it is more efficient. The String mechanism creates two new objects and then adds them together to make a third. The StringBuilder one just removes the section of the string.

public void test() {
    String a = "0123456789";
    StringBuilder b = new StringBuilder(a);
    System.out.println("a=" + a + " b=" + b);
    a = a.substring(0, 2) + a.substring(5);
    b.delete(2, 5);
    System.out.println("a=" + a + " b=" + b);
}
Answer from OldCurmudgeon on Stack Overflow
🌐
BeginnersBook
beginnersbook.com › 2014 › 08 › stringbuffer-substring-method-example
Java – StringBuffer substring method Example
October 9, 2022 - class JavaExample{ public static void main(String[] args) { StringBuffer sb = new StringBuffer("BeginnersBook"); //substring from 0 till 5 String subStr = sb.substring(0, 5); System.out.println("Substring from index 0 to 5: "+subStr); } }
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuffer_substring_end.htm
Java.lang.StringBuffer.substring() Method
package com.tutorialspoint; import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("admin"); System.out.println("buffer = " + buff); // prints substring from index 3 System.out.println("substring is = " + buff.substring(3)); ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-substring-method-in-java-with-examples
StringBuffer substring() method in Java with Examples - GeeksforGeeks
July 30, 2019 - Return Value: This method returns the substring lie in the range index start to index end-1 of old sequence. Exception: This method throws StringIndexOutOfBoundsException if start or end are negative or greater than length(), or start is greater than end. Below programs illustrate the StringBuffer.substring() method: Example 1:
🌐
University of Texas
cs.utexas.edu › ~mitra › csSummer2012 › cs312 › lectures › strings.html
String and StringBuffer
int indexOf ( String str, int fromIndex ) : returns the first occurrence of the substring starting from the fromIndex.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuffer_substring.htm
Java StringBuffer substring() Method
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuffer.html
StringBuffer (Java Platform SE 8 )
April 21, 2026 - The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point. For example...
🌐
Benchresources
benchresources.net › home › java › java – stringbuffer substring() method
Java - StringBuffer substring() method - BenchResources.Net
July 30, 2022 - substring() method throws StringIndexOutOfBoundsException, if index value passed falls out of range i.e.; if either start-index or end-index is negative (<0) ... subsequence() method throws IndexOutOfBoundsException, if index value passed falls out of range i.e.; if either start-index or end-index is negative (<0) ... package in.bench.resources.stringbuffer.methods; public class StringBufferSubstringMethod { public static void main(String[] args) { // StringBuffer 1: to get substring() - // starting from 10th index-position StringBuffer sb1 = new StringBuffer( "Java is a super cool language");
Find elsewhere
🌐
Java Guides
javaguides.net › 2024 › 06 › java-stringbuffer-substring-method.html
Java StringBuffer substring() Method
June 10, 2024 - The StringBuffer.substring() method in Java provides a way to extract a substring from a StringBuffer object and return it as a new String. By understanding how to use this method, you can efficiently work with specific portions of the character sequence without modifying the original StringBuffer.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-replace-method-in-java-with-examples
StringBuffer replace() Method in Java with Examples - GeeksforGeeks
December 13, 2021 - // Java program to illustrate the ... void main(String[] args) { StringBuffer sbf = new StringBuffer(&quot;Welcome to Geekshssgeeks&quot;); System.out.println(&quot;string buffer = &quot; + sbf); // Replacing substring from index ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.stringbuffer.substring
StringBuffer.Substring Method (Java.Lang) | Microsoft Learn
[Android.Runtime.Register("substring", "(II)Ljava/lang/String;", "")] public override string Substring(int start, int end);
🌐
Dremendo
dremendo.com › java-programming-tutorial › java-stringbuffer
StringBuffer in Java Programming | Dremendo
import java.util.Scanner; public ... 2 strings"); s1=sc.nextLine(); s2=sc.nextLine(); StringBuffer sb=new StringBuffer(s1); sb.append(s2); System.out.println(sb); } } ... The replace() method replaces a substring with the ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuffer.html
StringBuffer (Java Platform SE 7 )
The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point. For example...
🌐
HappyCoders.eu
happycoders.eu › java › substring
Java substring() Method
June 12, 2025 - ... substring appended to empty string: string identity : @20c10f string : Code value[] identity : @62eec8 value[] : [C, o, d, e, , , , , , , , , , , , ] offset : 0 count : 4 ...Code language: plaintext (plaintext) That's because, in these versions, a StringBuffer is used for concatenation, which is created with an initial length of 16 characters and whose toString() method returns a string using the same char array.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
April 21, 2026 - The class String includes methods ... substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class. The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and ...
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › getchar.html
Getting Characters by Index from a String or String Buffer
If you want to get more than one character from a string, string buffer, or string builder, you can use the substring method.
🌐
University of Washington
courses.cs.washington.edu › courses › cse341 › 98au › java › jdk1.2beta4 › docs › api › java › lang › StringBuffer.html
Class java.lang.StringBuffer
Removes the character at the specified position in this StringBuffer (shortening the StringBuffer by one character). ... This string buffer. ... StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length(). ... Replaces the characters in a substring of this StringBuffer with characters in the specified String.
🌐
Bureau of Economic Geology
beg.utexas.edu › lmod › agi.servlet › doc › detail › java › lang › StringBuffer.html
java.lang Class StringBuffer
Returns a new String that contains a subsequence of characters currently contained in this StringBuffer. The substring begins at the specified start and extends to the character at index end - 1.
🌐
Android Developers
developer.android.com › api reference › stringbuffer
StringBuffer | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体