pract7
pract7
Java StringBuilder class is used to create mutable (modifiable) String. The Java StringBuilder class is
same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.
Constructor Descrip on
Method Descrip on
public StringBuilder replace(int startIndex, int endIndex, It is used to replace the string from specified startIndex
String str) and endIndex.
AD
The StringBuilder append() method concatenates the given argument with this String.
StringBuilderExample.java
1. class StringBuilderExample{
6. }
7. }
Output:
Hello Java
The StringBuilder insert() method inserts the given string with this string at the given posi on.
StringBuilderExample2.java
1. class StringBuilderExample2{
5. System.out.println(sb);//prints HJavaello
6. }
7. }
Output:
HJavaello
The StringBuilder replace() method replaces the given string from the specified beginIndex and
endIndex.
StringBuilderExample3.java
1. class StringBuilderExample3{
4. sb.replace(1,3,"Java");
5. System.out.println(sb);//prints HJavalo
6. }
7. }
Output:
HJavalo
The delete() method of StringBuilder class deletes the string from the specified beginIndex to
endIndex.
StringBuilderExample4.java
1. class StringBuilderExample4{
4. sb.delete(1,3);
5. System.out.println(sb);//prints Hlo
6. }
7. }
Output:
Hlo
1. class StringBuilderExample5{
4. sb.reverse();
5. System.out.println(sb);//prints olleH
6. }
7. }
Output:
AD
olleH
The capacity() method of StringBuilder class returns the current capacity of the Builder. The default
capacity of the Builder is 16. If the number of character increases from its current capacity, it
increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.
StringBuilderExample6.java
1. class StringBuilderExample6{