JMK
JMK

Reputation: 28059

Does StringBuilder need ToString()

This should hopefully be a quick one. I have a StringBuilder like so:

StringBuilder sb = new StringBuilder();

I append to my StringBuilder like so:

sb.Append("Foo");
sb.Append("Bar");

I then want to make this equal to a string variable. Do I do this like so:

string foobar = sb;

Or like so:

string foobar = sb.ToString();

Is the former being lazy or the latter adding more code than is necessary?

Thanks

Upvotes: 5

Views: 23557

Answers (10)

Bimalesh Jha
Bimalesh Jha

Reputation: 1504

All previous answers are correct. However, wanted to highlight another usage, which, in some scenarios can be useful. When converting to string we can use built-in static functions/utilities e.g.

C#

string str = Convert.ToString(sb);

Java

String str = String.valueOf(sb);

This is particularly useful when input "sb" can be null, but, code wants to handle it gracefully without raising a NullPointerException and avoids code like:

if (sb == null) 
   return "null";
else
   return sb.toString()

Upvotes: 0

Bimalesh Jha
Bimalesh Jha

Reputation: 1504

In Java all objects have toString(), inherited from Object superclass. StringBuilder (SB) is mutable, String is not. Assigning SB (Mutable) to String (immutable) needs to copy the internal char buffer and return an immutable String instance.

Upvotes: 1

Harsh
Harsh

Reputation: 3751

String foobar = sb.ToString();

should be used. As this will convert the StringBuilder to String and then assign the value.

Using string foobar = sb; will give you a compile time error "Cannot implicitly convert type to "

Upvotes: 1

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8530

ToString() in StringBuilder required to convert to String.

String str=sb.toString();

Upvotes: 1

Azodious
Azodious

Reputation: 13872

When converting to String use toString in Java. (ToString in C#)

Upvotes: 1

Egor
Egor

Reputation: 40193

Previous answers are correct, just to clarify some things. StringBuilder is not a String's subclass so you can't cast from StringBuilder to String. StringBuilder's toString() method produces a String object, so you should use it to "cast" from StringBuilder to String. Hope this helps.

Upvotes: 1

Anders Wendt
Anders Wendt

Reputation: 369

In C# you need to use string foobar = sb.ToString(); or you will get an error: Cannot implicitly convert type 'System.Text.StringBuilder' to 'string'

Upvotes: 8

Jon Skeet
Jon Skeet

Reputation: 1500015

In Java, you can't define implicit conversions between types anyway, beyond what's in the specification - so you can't convert from StringBuilder to String anyway; you have to call toString().

In C# there could be an implicit user-defined conversion between StringBuilder and String (defined in either StringBuilder or String), but there isn't - so you still have to call ToString().

In both cases you will get a compile-time error if you don't call the relevant method.

Upvotes: 15

ken2k
ken2k

Reputation: 48975

You could have tested it before asking... You would have seen that

string foobar = sb;

doesn't compile (C#).

Use sb.ToString().

Upvotes: 2

Marcelo
Marcelo

Reputation: 4608

In Java, string foobar = sb; won't compile. You have to use String foobar = stringBuilder.toString();

Upvotes: 3

Related Questions