0% found this document useful (0 votes)
4 views

pract7

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

pract7

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java StringBuilder Class

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.

Important Constructors of StringBuilder class

Constructor Descrip on

It creates an empty String Builder with the ini al capacity


StringBuilder()
of 16.

StringBuilder(String str) It creates a String Builder with the specified string.

It creates an empty String Builder with the specified


StringBuilder(int length)
capacity as length.

Important methods of StringBuilder class

Method Descrip on

It is used to append the specified string with this string.


The append() method is overloaded like append(char),
public StringBuilder append(String s)
append(boolean), append(int), append(float),
append(double) etc.

It is used to insert the specified string with this string at


the specified posi on. The insert() method is overloaded
public StringBuilder insert(int offset, String s)
like insert(int, char), insert(int, boolean), insert(int, int),
insert(int, float), insert(int, double) etc.

public StringBuilder replace(int startIndex, int endIndex, It is used to replace the string from specified startIndex
String str) and endIndex.

It is used to delete the string from specified startIndex and


public StringBuilder delete(int startIndex, int endIndex)
endIndex.

public StringBuilder reverse() It is used to reverse the string.

public int capacity() It is used to return the current capacity.

It is used to ensure the capacity at least equal to the given


public void ensureCapacity(int minimumCapacity)
minimum.
public char charAt(int index) It is used to return the character at the specified posi on.

It is used to return the length of the string i.e. total


public int length()
number of characters.

It is used to return the substring from the specified


public String substring(int beginIndex)
beginIndex.

It is used to return the substring from the specified


public String substring(int beginIndex, int endIndex)
beginIndex and endIndex.

AD

Java StringBuilder Examples

Let's see the examples of different methods of StringBuilder class.

1) StringBuilder append() method

The StringBuilder append() method concatenates the given argument with this String.

StringBuilderExample.java

1. class StringBuilderExample{

2. public sta c void main(String args[]){

3. StringBuilder sb=new StringBuilder("Hello ");

4. sb.append("Java");//now original string is changed

5. System.out.println(sb);//prints Hello Java

6. }

7. }

Output:

Hello Java

2) StringBuilder insert() method

The StringBuilder insert() method inserts the given string with this string at the given posi on.

StringBuilderExample2.java

1. class StringBuilderExample2{

2. public sta c void main(String args[]){

3. StringBuilder sb=new StringBuilder("Hello ");

4. sb.insert(1,"Java");//now original string is changed

5. System.out.println(sb);//prints HJavaello
6. }

7. }

Output:

HJavaello

3) StringBuilder replace() method

The StringBuilder replace() method replaces the given string from the specified beginIndex and
endIndex.

StringBuilderExample3.java

1. class StringBuilderExample3{

2. public sta c void main(String args[]){

3. StringBuilder sb=new StringBuilder("Hello");

4. sb.replace(1,3,"Java");

5. System.out.println(sb);//prints HJavalo

6. }

7. }

Output:

HJavalo

4) StringBuilder delete() method

The delete() method of StringBuilder class deletes the string from the specified beginIndex to
endIndex.

StringBuilderExample4.java

1. class StringBuilderExample4{

2. public sta c void main(String args[]){

3. StringBuilder sb=new StringBuilder("Hello");

4. sb.delete(1,3);

5. System.out.println(sb);//prints Hlo

6. }

7. }

Output:

Hlo

5) StringBuilder reverse() method

The reverse() method of StringBuilder class reverses the current string.


StringBuilderExample5.java

1. class StringBuilderExample5{

2. public sta c void main(String args[]){

3. StringBuilder sb=new StringBuilder("Hello");

4. sb.reverse();

5. System.out.println(sb);//prints olleH

6. }

7. }

Output:

AD

olleH

6) StringBuilder capacity() method

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{

2. public sta c void main(String args[]){

You might also like