Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Java StringBuilder

Java StringBuilder

April 30th, 2019

Java StringBuilder

Definition of String Builder

StringBuilder synchronized of multiple threads objective that can be executed one build after others provide us reliable resulting value.
StringBuilder is the executing faster than the StringBuffer value and since it does not take the time for synchronization.
StringBuilder – > class introduction JDK 1.5 version.
Both of the StringBuffer and StringBuilder objects value are mutable, unlike to the String object value which is unmutable.
StringBuilder is the method of classified similar to those of StringBuffer class value.

How to Create String-Builder Objects:

StringBuilder object Defined as a simply uses of new object value and bypassing through the string value and it’s inside to the StringBuilder Constructor object.

StringBuilder <object Value> = new StringBuilder(String_value)

Example Code:

StringBuilder sb = new StringBuilder(“HelloWorld”);
To create an empty object on String-Builder and then adding the value of two way of creating methods,

  • String-Builder appends
  • String-Builderinsert

String-Builder append :

The java.lang.StringBuilder.append is a boolean value inbuilt method in Java it’s can be done to append this string value type of the boolean argument value function to a sequence value.


public StringBuilder append(boolean a)
Sample Code
//StringBuilder append(boolean a) import java.lang.*;
public class Geeks {
public static void main(String[] args)
StringBuilder sb1 = new StringBuilder("GangBoard");
System.out.println("Input: " + sb1);
//Appending the boolean value sb1.append(true);
System.out.println("Output: " + sb1);
StringBuilder sb2 = new StringBuilder("We fail- ");
System.out.println("Input: " + sb2);
//Appending the boolean value sb2.append(false); System.out.println("Output: " + sb2);

Insert Appending Value of String using StringBuilder

Before inserting value/appending a string value, an empty object to be a created String-Builder.

//Java program to illustrate the
//append(char[] cstr, int iset, int ilength) import java.lang.*;
public class GangBoard{
public static void main(String[] args)
StringBuilder sb = new StringBuilder("GangBoard");
System.out.println("String Builder "+ "before = " + sb);
char[] cstr = new char[]
{ 'f', 'o', 'r', 'B', 'e', 's', 'a', 'n,'t' };
sb.append(cstr, 0, 8);

Delete characters Value of the StringBuilder:

Deletion and insertion are similar.


StringBuilder delete(int x1, int x2)
This would delete the value of the character starting from the position x1 till x2-1.


StringBuilder name= new StringBuilder();
names.delete(6,15); // deletes Technologies from name, name would contain GangBoard

Replacing Character String Using StringBuilder

The sequence of Characters can be returned value with a new set of records using the replace() method.


StringBuilder replace(int i, int j, String s)

Finding the length value of String Function Using StringBuilder


int length()


StringBuilder name= new StringBuilder(“HelloWorld”);
int l = name.length();
// l would contain 10

Sub-string from the string using StringBuilder

Retrieves the attributes starting from the position I from the StringBuilder object.

StringBuilder substring(int i);

Index of StringBuilder Object Character:

Position of a character(s) can do discovered relating indexOf() method. Returns the first value appearance to the substring of left to right.

int indexOf(String str)