use can use constructor of the StringBuilder

StringBuilder builder = new StringBuilder(string.substring(start, end));
Answer from mss on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-substring-method-in-java-with-examples
StringBuilder substring() method in Java with examples - GeeksforGeeks
December 19, 2025 - The extracted substring "For" is returned as a new String, and the original StringBuilder sb remains unchanged. Passing a negative index or an index greater than the length of the StringBuilder causes a StringIndexOutOfBoundsException at runtime. ... class GFG{ public static void main(String[] args){ StringBuilder sb = new StringBuilder("JavaDeveloper"); try { sb.substring(-3); } catch (Exception e) { System.out.println(e); } } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
April 21, 2026 - public StringBuilder replace(int start, int end, String str) Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character ...
🌐
Codecademy
codecademy.com › docs › java › stringbuilder › .substring()
Java | StringBuilder | .substring() | Codecademy
August 22, 2022 - The .substring() method will return the substring starting at the zero-indexed int start index to the character specified at index end - 1. If the int end is omitted, the substring will extend from start to the end of the sequence.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_substring.htm
Java StringBuilder substring() Method
The Java StringBuilder substring() method is, used to retrieve a desired subsequence from a StringBuilder 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
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.stringbuilder.substring
StringBuilder.Substring Method (Java.Lang) | Microsoft Learn
Microsoft makes no warranties, express or implied, with respect to the information provided here. Returns the String value of the subsequence from the start index to the end index.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java stringbuilder substring() method
Java.lang.StringBuilder.substring() Method
February 13, 2026 - The java.lang.StringBuilder.substring(int start, int end) method returns a new String that contains a subsequence of characters currently contained in this sequence.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-substring
Java StringBuilder substring()
class JavaExample{ public static void main(String[] args) { StringBuilder sb = new StringBuilder("BeginnersBook"); //substring from 0 till 5 String subStr = sb.substring(0, 5); System.out.println("Substring from index 0 to 5: "+subStr); } }
🌐
TutorialKart
tutorialkart.com › java › java-stringbuilder-substring
Java StringBuilder.substring() - Syntax & Examples
January 24, 2021 - StringBuilder.substring() returns a new String that contains a subsequence of characters, formed using start position and an optional end position, contained in this character sequence.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 669328 › java › StringBuilder-substring
StringBuilder and substring() [Solved] (Beginning Java forum at Coderanch)
OK, so here is the code: Right, this is the way I thought I'd have solved it. sub essentially contains 'anim', but what I thought was that this would have had an effect even on sb itself, changing its value from "animals" to "anim", reason being there is only one StringBuilder object which isn't immutable so I don't need to store its reference anywhere: a bit as if I would have done this sb.append("b"), which would have changed the string to "animalsb" presumably? ... The substring method of class StringBuilder does not change the StringBuilder, so you're wrong about the last part.
Top answer
1 of 3
3

A practical reason is that it makes things easier in some common situations:

  • If you want the operation to include everything until the end of the string, you can directly use the length as endIndex
  • If you have "separation characters", like the dot between base name and filetype suffix, you typically don't want to include them, so you can use the index of the separation character as endIndex, e.g. String basename = filename.substring(0, filename.indexOf('.'))
2 of 3
1

I agree with Micheal Borgwardt's answer but there is an opportunity for elaboration. The approach you see here is not only useful but also consistent with most the APIs you will find in Java and many other languages. The association with 0-based indexing is more clear if you think about the standard old-style for-loop: for (int i = 0; i < end; i++). These days we tend to favor for-each style constructs but this is how a lot of code is written. Note the while condition, it's based on and exclusive end with a < instead of a <=. When you are writing a lot of loops like this, it's helpful to use a similar construct each time.

But to really understand the bigger picture on this, I think it helps to look at a different kind of problem: time intervals. Let's imagine I want to tell if something happened today, in the morning. If I structure this as an inclusive end, I then have a problem of figuring out what the last time possibly could be. That means I need to know the smallest resolution of the clock. Is it seconds, milliseconds, microseconds, nanoseconds? Let's say I think seconds is good enough. So I say 'less than or equal to 11:59'. Does that work if it happened at 11:59 and 30 seconds? Well, I'll check whether "the minute of the day was less than or equal to 11:59". Or I could just say "less than noon" which is a lot more elegant and tends to be more robust.

Or consider date periods. How do I know if two date periods are adjacent i.e. that there is no gap in between? If I used an exclusive end, it's trivial. I just check that the (exclusive) end on the first period is equal to or after the (inclusive) start of the second. If I use an inclusive end, now I have to get out a calendar and figure out if the day after September 30th is October 1st. And does March 1st follow Feb 28? What year is it? Divide that by 4 unless we are talking about the last year of the century but don't forget that the year 2000 is the exception to the exception. There are strategies to solve that kind of thing but the easiest is to convert the inclusive end to exclusive.

🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 7 )
public StringBuilder replace(int start, int end, String str) Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character ...
🌐
Java Tutorial HQ
javatutorialhq.com › java tutorial › java.lang › stringbuilder › substring() method example
Java StringBuilder substring() method example
September 30, 2019 - package com.javatutorialhq.java.examples; /* * A java example source code to demonstrate * the use of substring() method of StringBuilder class */ public class StringBuilderSubstringExample { public static void main(String[] args) { // initialize the StringBuilder object StringBuilder sb = new StringBuilder("test string abcd1234"); System.out.println("Contents of buffer:" + sb); /* * get the substring from this StringBuilder * starting from * index 2 to index 10 */ int start = 2; int end = 10; String str = sb.substring(start, end); System.out.println("Results:" + str); } }
🌐
Java Guides
javaguides.net › 2024 › 06 › java-stringbuilder-substring-method.html
Java StringBuilder substring() Method
June 10, 2024 - The StringBuilder.substring() method in Java is used for extracting a portion of the character sequence from a StringBuilder object and returning it as a new string. By understanding how to use the overloaded methods, you can efficiently work ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-class-in-java-with-examples
StringBuilder Class in Java - GeeksforGeeks
Explanation : This program demonstrates various StringBuilder methods such as append(), insert(), replace(), delete(), reverse(), substring(), and setCharAt() to perform dynamic string manipulation efficiently in Java. The table below demonstrates the difference between String, StringBuilder and StringBuffer: Performs faster string manipulations in single-threaded environments.
Published   May 8, 2026
🌐
Great Learning
mygreatlearning.com › blog › it/software development › java stringbuilder class: methods, examples and more
Java StringBuilder Class: Methods, Examples and more
September 12, 2024 - The above program replaces the substring specified with index numbers from the main string with another substring. This way, you can replace the characters or the whole string using the index number of the main string. ... public class DeleteMethodEg{ public static void main (String args[]){ StringBuilder mystring = new StringBuilder(“GreatLearning”); mystring.delete(0,5); System.out.println(mystring); } }
🌐
Scaler
scaler.com › home › topics › java › stringbuilder in java with examples, methods and constructors
StringBuilder in Java with Examples, Methods, and Constructors - Scaler Topics
May 3, 2023 - StringBuilder.substring(start index,endingindex+1): This method takes two parameters to start index of the sequence and the ending index of the sequence.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-subsequence-in-java-with-examples
StringBuilder subSequence() in Java with Examples - GeeksforGeeks
April 23, 2023 - // Java program to demonstrate // Exception thrown by the subSequence() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("Indian Team Played Well"); try { // get subSequence between index 0 to 7 // using subSequence() and print System.out.println(str.subSequence(19, 18)); } catch (Exception e) { e.printStackTrace(); } } } Output: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:935)
🌐
Quora
quora.com › Is-the-substring-method-of-StringBuilder-class-immutable
Is the substring() method of StringBuilder class immutable? - Quora
With mutable StringBuilders that were temporarily identical, you could not throw one away and replace it with a reference to the other, since at any moment their values could diverge again. You can create substrings without copying. You just create a pointer into an existing base Stringguaranteed never to change. Immutability is the secret behind Java...
🌐
W3Schools
w3schools.com › java › ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The substring() method returns a substring from the string.