Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Empty StringBuffer in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 08-03-2010
Member
 
Join Date: Nov 2009
Posts: 344
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.
Reply With Quote
  #3  
Old 08-03-2010
Member
 
Join Date: Nov 2009
Posts: 447
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.
Reply With Quote
  #4  
Old 08-03-2010
Member
 
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.
Reply With Quote
  #5  
Old 08-03-2010
Member
 
Join Date: Nov 2009
Posts: 331
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Empty StringBuffer in java"
Thread Thread Starter Forum Replies Last Post
Can't empty recycle bin Norman Stein Windows XP Support 11 19-12-2011 01:40 AM
JAVA/Stutter.J.2 is a virus or any kind of java file GaganjyotTechie Networking & Security 6 29-05-2011 10:55 AM
Differentiation among StringBuffer and StringBuilder class Ram Bharose Software Development 5 27-01-2010 02:20 PM
Difference between String and StringBuffer. Luz Software Development 3 01-12-2009 10:13 PM
Link List Example in Java Sample program in Java trickson Software Development 2 04-08-2009 09:23 PM


All times are GMT +5.5. The time now is 11:28 AM.