Back to photostream

StringBuilder Java

StringBuilder in Java

StringBuilder class in Java is used to manipulate strings so that we can modify the value. In other, StringBuilder class is mutable. It is similar to StringBuffer and String class except that this is mutable whereas StringBuffer is immutable. The performance of StringBuilder is faster than StringBuffer and does not support multiple threads and hence is non-synchronized. In this tutorial, we will learn about the StringBuilder class and methods like append, reverse, delete,toString etc using StringBuilder Java with examples.

 

Constructors of Java StringBuilder

Below are the constructors of the StringBuilder class:

 

Methods of StringBuilder class in Java

Below are the StringBuilder methods:

 

Java StringBuilder Examples

In this section, we will see about the StringBuilder Java class and methods using StringBuilder Java with examples.

Example: insert() method

We can insert a specified string at the required position using the StringBuilder Java method which is insert(). The below example inserts the new string at index 2.

public class StringBuilderDemo {

 

public static void main(String args) {

StringBuilder sb = new StringBuilder("Java");

sb.insert(2, "Hello");

 

System.out.println(sb);

 

}

 

}

 

JaHellova

 

Example: append() method

The StringBuilder append method in Java appends the new string to the existing string at the end. In this example, we add a new string "language" to the existing string "Java".

 

www.tutorialcup.com/java/stringbuilder.htm

26 views
0 faves
0 comments
Uploaded on December 20, 2021