Results 1 to 5 of 5

Thread: Empty StringBuffer in java

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Empty StringBuffer in java

    Hello,
    Is there a clean and efficient way to clear an instance of StringBuffer. I have a loop in which I use a StringBuffer to avoid creating too many instances of String, but each iteration of the loop I would have my new empty StringBuffer. Any help on this will be appreciated. Thanks for your help.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Empty StringBuffer in java

    Hello,
    I do not know what exactly you are using the string buffer for, I have a code with me, see if this helps you.
    Here is the code
    Code:
    StringBuffer d = new StringBuffer ( "ghjghj");
    d.append ( "dsdfsdf"). append ( "sdfsdf");
    
    / / Empty StringBuffer
    d.delete (0, d.length ());
    Hope this will help you.

  3. #3
    Join Date
    Nov 2009
    Posts
    446

    Re: Empty StringBuffer in java

    Hello,
    Another way is to use the method setLength like this, I do not know that this is what you need.
    Here is the method you can use it in your code
    Code:
    mnstring.setLength(0);
    What is the expected result of your code? that is what your program needs. I mean to say what is the output you want from your program.

  4. #4
    Join Date
    Dec 2009
    Posts
    263

    Re: Empty StringBuffer in java

    Hello,
    I think I misunderstood the delete method, according to the JavaDoc, I thought it returned an stringBuffer, but really did not erase the original stringBuffer. The alternative seems to me very well or I think this is easy to implement in my code, i would rather go with the same. By the way thanks for your replies they helped me a lot and also if you have some more interesting information on the this then please do post back.

  5. #5
    Join Date
    Nov 2009
    Posts
    330

    Re: Empty StringBuffer in java

    Hello,
    Most methods StringBuffer return a StringBuffer which in fact corresponds to the current object. In code this is summarized by the following code:
    Code:
    return this;
    This allows you to stack calls that same subject:
    Code:
    sb.append("Hello").append(" ").append("World");
    Hope the above part of the code will help you. If you need any more explanation regarding this topic then you are free to ask.

Similar Threads

  1. Trash cannot be empty on Mac OS
    By fflygirl in forum Operating Systems
    Replies: 3
    Last Post: 16-09-2011, 08:57 PM
  2. Recycle Bin Always Empty
    By Potawatomi in forum Operating Systems
    Replies: 5
    Last Post: 19-10-2010, 04:03 AM
  3. OS X 10.5.6 Trash can not empty
    By Max_Miraa in forum Operating Systems
    Replies: 4
    Last Post: 10-04-2010, 01:28 PM
  4. Differentiation among StringBuffer and StringBuilder class
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 02:20 PM
  5. Difference between String and StringBuffer.
    By Luz in forum Software Development
    Replies: 3
    Last Post: 01-12-2009, 10:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,111,013.43622 seconds with 16 queries