Square CSS

Monday, October 8, 2018

StringBuffer in Java

A StringBuffer is like String but it can be modified because of its mutable nature. They are thread-safe and can be easily used in a multithreaded environment.

It has two important methods append() and insert().

The append("x") function appends "x" at the end of existing StringBuffer.
The insert(5, "x") function inserts the string "x" at the provided index of existing StringBuffer. In this case, it is 5.


  
   StringBuffer sb=new StringBuffer("Initial");  //Initial
   sb.append("Appended");  //InitialAppended
   sb.insert(7, "Inserted");   //InitialInsertedAppended         


To convert a StringBuffer to String we use toString() method. 


  String str = sb.toString();

                                                                      


☛ Next >> StringBuilder in Java
                      
Unlike String, StringBuilder is mutable in nature. The API of this cla ...

No comments:

Post a Comment

Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts