Square CSS

Monday, October 8, 2018

StringBuilder in Java

Unlike String, StringBuilder is mutable in nature. The API of this class is compatible with StringBuffer except that the StringBuilder is unsafe in a multithreaded environment. It is not synchronized so should it should not be used where thread safety is of prime concern.


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

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


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



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


  String str = sb.toString();

                                                              


☛ Next >> String vs StringBuffer vs StringBuilder
                     
Let's have some basic idea about StringStringBuffer and StringBuilder. ...

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