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().
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.
Convert the string into stringBuilder in java - Stack Overflow
In Java, why use StringBuilder instead of Strings?
How to convert from string to stringbuilder? - Post.Byes
[Java] What exactly is stringBuilder and why use it instead of a traditional String?
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?