String Builder


Java String Builder also performs same like String buffer Class and create mutable and modifiable Strings except that the process is not synchronized. So String builder Class I available from JDK 15.

String Builder Consist of 3 Constructors

1) StringBuilder() → which create a String Builder of length 16.

2) StringBuilder(int length) → this constructor creates a string builder of given length .

3) StringBuilder(String str)→ this Constructor creates String Builder of the string parameter.



String builder also provides few methods like String Buffer and few of those methods are stated below with example→

1)Length() method:

The Length Method of String Builder Class returns the ttal count of character in the string.

2)Capacity() method:

The capacity method of String Builder returns the size of String builder as it Default capacity when initialize is 16 and increases based on (previous_Capacity * 2) +2 , as string gets append.

3) Replace() method:

the Replace String Builder replaces the string characters with the string passed as an argument based on the start index and end index passed as argument.

Example:

Com.knowledge2life public class Main{ public static void main(String args[]){ StringBuilder sb=new StringBuilder("Knowledge"); sb.append("2life"); System.out.println(sb); } }

OUTPUT:

Knowledge2life