To answer the question directly, you can use streams to convert your StringBuilder[] to String[] easily enough:

String[] arr = Arrays.stream(sbCmd).map(StringBuilder::toString).toArray(String[]::new);

...but:

I'm trying to avoid String[] here because it is immutable (I have some sensitive data which needs to be cleared after function call).

You can't avoid it. If you have a library function that takes a String array, then you have to provide a String array - and that obviously requires the use of Strings.

Your only other option is to fork the library and remove the use of strings. Note that this is likely to be a time-consuming, and tedious operation however - it's not nearly as simple as just making sure no string parameters are passed in (as the library function could easily call sb.toString() and obtain a string.)

The much more pragmatic approach would be not to worry about strings being left in memory, instead running your code in a secure, isolated environment, and letting the environment do its job.

Answer from Michael Berry on Stack Overflow
🌐
Blogger
javahungry.blogspot.com › 2021 › 09 › stringbuilder-to-string-java.html
How to Convert a StringBuilder to a String in Java | Java Hungry
Using Constructor Read Also: String Interview Questions and Answers · The easiest way to convert StringBuilder to String is by invoking the toString() method of the StringBuilder class. Below are the steps: a. Create an object of StringBuilder class b. Append the data to the StringBuilder ...
🌐
coderolls
coderolls.com › convert-stringbuilder-to-string-in-java
How To Convert StringBuilder To String In Java? - Coderolls
December 24, 2020 - /** * A Java program to convert StringBuilder to String * @author coderolls at cderlls.com * */ public class StringBuilderToString { public static void main(String[] args) { StringBuilder stringBuilder = new StringBuilder("Hello "); stringBuilder.append("Gaurav ") .append("Kukade!"); System.out.println("Printing strinBuilder as String: \n"); System.out.println(stringBuilder.toString()); } }
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-tostring
Java StringBuilder toString()
October 4, 2022 - public class JavaExample { public ... {'a','e','i','o','u'}; //append array to StringBuilder sb.append(ch); //convert StringBuilder to String String str = sb.toString(); System.out.println("String: "+str); } }...
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-tostring-method-in-java-with-examples
StringBuilder toString() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Now we are done with discussing ... by converting it to a single string using to.String() method. Here we will create a StringBuilder class object then we will store the array of string as its object.
🌐
TutorialsPoint
tutorialspoint.com › converting-a-stringbuilder-to-string-in-java
Java StringBuilder toString() Method
September 2, 2023 - Using the toString() method, we are trying to retrieve the string representation of the object. public class Demo { public static void main(String[] args) { //creating an object of the StringBuilder class StringBuilder sb = new StringBuilde...
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › how to convert string to stringbuilder and vice versa in java
How to Convert String to StringBuilder and Vice Versa in Java | Baeldung
April 7, 2025 - In Java, Strings are immutable, meaning once an object is created, it cannot be changed. On the other hand, StringBuilder is a mutable sequence of characters, which allows us to modify the contents without creating new objects. Therefore, we often convert a String to a StringBuilder for manipulations and then convert the StringBuilder object back to a String to obtain the result.
🌐
Brainly
brainly.in › computer science › secondary school
How to convert string to stringbuilder in java - Brainly.in
June 14, 2023 - It's important to note that `StringBuilder` is mutable, meaning you can modify its content. If you need an immutable version, you can use the `String` class instead.
Top answer
1 of 2
6

The toString implementation currently just redirects to the result method anyway, so those two methods will behave in the same way. However, they express slightly different intent:

  • toString requests a textual representation of StringBuilders current state that is "concise but informative (and) that is easy for a person to read". So, theoretically, the (vague) specification of this method does not forbid abbreviating the result, or enhancing conciseness and readability in any other way.
  • result requests the actual constructed string. No different readings seem possible here.

Therefore, if you want to obtain the resulting string, use result to express your intent as clearly as possible.

In this way, the reader of your code won't have to wonder whether StringBuilder.toString might shorten something for the sake of "conciseness" when the string gets over 9000 kB long, or something like that.

The mkString is for something else entirely, it's mostly used for interspersing separators, as in "hello".mkString(",") == "h,e,l,l,o".

Some further links:

  • The paragraph with "hashcode in hexadecimal" describes the default. It is just documentation inherited from AnyRef, because the creator of StringBuilder didn't bother to provide more detailed documentation.
  • If you look into code, you'll see that toString is actually just delegating to result.
  • The documentation of StringBuilder also mentions result() in the introductory overview paragraph.

Just use result().

2 of 2
2

TL;DR; use result as stated in the docs.

toString MUST never be called in anything at all for another purpose other than a quick debug.

mkString is inherited from collections hierarchy and it will basically create another StringBuilder so is very inefficient.

🌐
Benchresources
benchresources.net › home › java › java – how to convert string to stringbuilder and vice-versa ?
Java - How to convert String to StringBuilder and vice-versa ? - BenchResources.Net
October 8, 2022 - "); // 2. again, append some more string values sb.append("To get latest topics on Core Java."); // 3. third time, append String-3 and // add newline '\n' sb.append("\nAnd it can search contents in real-time."); // convert StringBuilder to String using toString() method String str = sb.toString(); ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
String and StringBuilder Converting Example - Java Code Geeks
October 7, 2024 - Line 38: StringBuilderToString_valueOf – converts StringBuilder to String via the static valueOf method. In this step, I will run the java application StringBuilderConvertor.
🌐
Geekster
geekster.in › home › stringbuilder in java
StringBuilder in Java (with Example)
April 3, 2024 - This can result in improved performance, especially for tasks involving extensive string concatenation or modification. ... Yes, you can convert a StringBuilder object to a String using its toString() method.
🌐
GitHub
gist.github.com › gauravkukade › 00bd416bcb2ca1c72dbcd600d48e2f78
A Java program to convert StringBuilder to String. Read How to convert StringBuilder to String i Java : coderolls.com/convert-stringbuilder-to-string-in-java · GitHub
A Java program to convert StringBuilder to String. Read How to convert StringBuilder to String i Java : coderolls.com/convert-stringbuilder-to-string-in-java - StringBuilderToString.java
🌐
Codecademy
codecademy.com › docs › java › stringbuilder
Java | StringBuilder | Codecademy
April 24, 2025 - This example shows different ways to create a StringBuilder and illustrates that the default capacity is 16 characters if not specified. It also demonstrates how to convert a StringBuilder back to a regular String using the .toString() method.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › buffers.html
The StringBuilder Class (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
Note: You can use any String method on a StringBuilder object by first converting the string builder to a string with the toString() method of the StringBuilder class.
🌐
IONOS
ionos.com › digital guide › websites › web development › java stringbuilder
How to use Java’s StringBuilder - IONOS
January 3, 2025 - In Java, you can convert the primitive data type integer into the complex data type string. There are a couple of things to pay attention to when making this con­ver­sion, to ensure that it goes smoothly.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-string-to-stringbuilder-and-vice-versa-java
How to convert String to StringBuilder and vice versa Java?
July 2, 2020 - To convert a StringBuilder to String value simple invoke the toString() method on it. In the following Java program we are converting an array of Strings to a singleStringusing the toString() method of the StringBuilder.