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 Overflowjava - How to convert StringBuilder[] to String[] - Stack Overflow
scala - What's the correct way to convert from StringBuilder to String? - Stack Overflow
Convert the string into stringBuilder in java - Stack Overflow
In Java, why use StringBuilder instead of Strings?
Videos
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:
toStringrequests a textual representation ofStringBuilders 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.resultrequests 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 ofStringBuilderdidn't bother to provide more detailed documentation. - If you look into code, you'll see that
toStringis actually just delegating toresult. - The documentation of
StringBuilderalso mentionsresult()in the introductory overview paragraph.
Just use result().
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.
To create a StringBuilder from a String simply:
StringBuilder sb = new StringBuilder("MyString");
String s = sb.toString();
But as was said StringBuilder and StringBuffer are different. Take a look here for more information on StringBuilder and StringBuffer
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
Difference between StringBuilder and StringBuffer
Convert String to StringBuilder
String someText = "something...";
StringBuilder sb = new StringBuilder(someText);
So far can't really tell the benefit of using it over Strings.
Going through firecode.io and saw this solution (to replace spaces with a certain string)
public static String replace(String a, String b) {
String ans = "";
for (char c: a.toCharArray() ){
if (c == ' '){ ans += b; }
else { ans += c; }
}
return ans;
}A user commented and said:
Use StringBuilder instead - it's more efficient! String does not allow appending. Each method you invoke on a String creates a new object and returns it. This is because String is immutable - it cannot change its internal state. On the other hand StringBuilder is mutable. When you call append(..) it alters the internal char array, rather than creating a new string object.
I'm curious about this and wanted to ask (I'm new to Java):
-
If a traditional 'String' is immutable, why am I able to use it in a "+=" operation to append a char at the end?
-
So when you use the "+=" operation, the computer instantiates a brand new String object each and every time you do it? So effectively I'm creating/destroying multiple String objects over and over again with every "+="?
-
Coming from C++, I see a lot of the "+=" operation when working with strings. Is it the same situation in C++ as it is expressed here in Java?