Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String and StringBuffer conversions

Status
Not open for further replies.

Elleri

Programmer
May 23, 2001
2
US
What would be the best way to convert string concatenations to stringbuffer appends. I know if has a performance impact overall. For example, I want to output to the user: The count was somenumber.

Here's my line

System.out.println ("The count was " + countvariable + ".\n");

Where countvariable is an int.

And do I have to convert stringbuffers back to strings?
 
Hi,

I think this is the best way to concatenate Strings:

StringBuffer sb = new StringBuffer(0);

sb.append("The count was").append(countvar).append(".\n");
System.out.println(sb);

Or if you want to convert StingBuffer back to String, simply use:
String str = sb.toString();

-Vepo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top