Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Easiest way to convert String to StringBuffer

  1. #1
    unideal is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default 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
    Java Code:
    thisMethodTakesString(myStringBuffer.toString());
    However, I have method that takes StringBuffer as an argument, but my variable is String.

    Right now I'm doing
    Java Code:
    String str = "Hello Java-forums.org";
    /* This is where it goes kinda complicated and messy */
    StringBuffer sb = new StringBuffer(str);
    thisMethodTakesStringBuffer(sb);
    Is there any simple way to cast String -> StringBuffer without needing to write those needless lines of code?

    Thanks.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  3. #3
    unideal is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    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
    Java Code:
    thisMethodTakesStringBuffer( StringBuffer(str) );
    and apparently it doesnt work.

    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?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unideal View Post
    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
    Java Code:
    thisMethodTakesStringBuffer( StringBuffer(str) );
    and apparently it doesnt work.

    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?
    A constructor always needs the 'new' operator:

    Java Code:
    thisMethodTakesStringBuffer(new StringBuffer(str));
    kind regards,

    Jos
    Nazneen Ali likes this.

  5. #5
    unideal is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    A constructor always needs the 'new' operator:

    Java Code:
    thisMethodTakesStringBuffer(new StringBuffer(str));
    kind regards,

    Jos
    Thanks! That is what I was looking for ;)

Similar Threads

  1. Replies: 1
    Last Post: 08-13-2009, 08:10 PM
  2. Replies: 3
    Last Post: 03-04-2009, 06:03 AM
  3. Replies: 1
    Last Post: 03-03-2009, 05:27 AM
  4. convert a really big string (len 39) to bigdecimal
    By coolsig in forum Advanced Java
    Replies: 6
    Last Post: 06-14-2008, 02:48 AM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •