Results 1 to 5 of 5
- 12-13-2009, 11:52 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Easiest way to convert String to StringBuffer
Hello lads!
Im looking a way to easily convert String to StringBuffer for passing it to different methods.
If it were the other way, I would use
However, I have method that takes StringBuffer as an argument, but my variable is String.Java Code:thisMethodTakesString(myStringBuffer.toString());
Right now I'm doing
Is there any simple way to cast String -> StringBuffer without needing to write those needless lines of code?Java Code:String str = "Hello Java-forums.org"; /* This is where it goes kinda complicated and messy */ StringBuffer sb = new StringBuffer(str); thisMethodTakesStringBuffer(sb);
Thanks.
- 12-13-2009, 12:11 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Did you read the API documentation for the StringBuilder (or StringBuffer) class? They both have constructors that do exactly what you want. Your code snippet already shows its usage.
kind regards,
Jos
- 12-13-2009, 12:23 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
I did. I know there is constructor StringBuffer(String str) for creating new StringBuffer object and then assigning the String value for it.
Is there however, shorter way to do it?
I tried
and apparently it doesnt work.Java Code:thisMethodTakesStringBuffer( StringBuffer(str) );
Is it necessary to make one more, needless StringBuffer line with kind of an dummy variable sb that I posted on the first post, or is there some easy way to convert it like StringBuffer's toString() is?
- 12-13-2009, 12:33 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 12-13-2009, 12:43 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
What is the easiest and most efficient vision library...
By jakx12 in forum Advanced JavaReplies: 1Last Post: 08-13-2009, 08:10 PM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum Advanced JavaReplies: 3Last Post: 03-04-2009, 06:03 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum New To JavaReplies: 1Last Post: 03-03-2009, 05:27 AM -
convert a really big string (len 39) to bigdecimal
By coolsig in forum Advanced JavaReplies: 6Last Post: 06-14-2008, 02:48 AM -
convert string to a double?
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-27-2007, 03:10 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks