Categories: Java

StringBuilder class in Java

  • The Java class StringBuilder represents a mutable string of characters.
  • The StringBuilder class offers a substitute for the String Class in Java since it constructs a mutable sequence of characters instead of an immutable one as the String Class does.
  • The functions of the StringBuilder and StringBuffer classes are extremely similar because both create changeable sequences of characters as an alternative to the String Class.
  • Except for being non-synchronized, the Java StringBuilder class is identical to the StringBuffer class.
Constructors of StringBuilder class:
  • StringBuilder(): It generates a blank String Builder with a 16-character initial capacity.
  • StringBuilder(String str): The provided string is used to generate a String Builder.
  • StringBuilder(int length): It generates a new String Builder that is empty and has the given capacity and length.
A simple example illustrates the use of a constructor:
// Java Code to illustrate StringBuilder

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo {
 public static void main(String[] argv) throws Exception
 {
  // Create a StringBuilder object
  // using StringBuilder() constructor
  StringBuilder str = new StringBuilder();

  str.append("Coderzpy");

  // print string
  System.out.println("String = " + str.toString());

  // create a StringBuilder object
  // using StringBuilder(CharSequence) constructor
  StringBuilder str1
   = new StringBuilder("AAAABBBCCCC");

  // print string
  System.out.println("String1 = " + str1.toString());

 }
}
Output:
String = Coderzpy
String1 = AAAABBBCCCC
Methods of StringBuilder class:
  • public StringBuilder append(String s): This string is utilized to append to the provided string. Like append(char), append(boolean), append(int), append(float), append(double), etc., the append() function has many overloads.
  • public StringBuilder insert(int offset, String s): It is used to insert the specified string with this string at the specified position. There are several other overloaded versions of the insert() method, including insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double), etc.
  • public StringBuilder replace(int startIndex, int endIndex, String str): The string from the startIndex and endIndex values are replaced using this method.
  • public StringBuilder delete(int startIndex, int endIndex): It is used to remove the string from startIndex and endIndex that are supplied.
  • public StringBuilder reverse(): It is used to reverse the string.
  • public int capacity(): The current capacity is returned using it.
  • public void ensureCapacity(int minimumCapacity):To make sure the capacity is at least equal to the required minimum, it is used.
  • public char charAt(int index): The character at the requested place is returned using it.
  • public int length(): The length of the string, or the total number of characters, is returned using this method.
  • public String substring(int beginIndex): The substring starting at the supplied beginIndex is returned using it.
  • public String substring(int beginIndex, int endIndex): It is used to retrieve the substring starting at the beginIndex and endIndex values supplied.
Simple code to illustrate the use of StringBuilder class methods:
// Java code to illustrate
// methods of StringBuilder

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo1 {
 public static void main(String[] argv)
  throws Exception
 {

  // create a StringBuilder object
  // with a String pass as parameter
  StringBuilder str
   = new StringBuilder("Coderzpy.com");

  // print string
  System.out.println("String = "
      + str.toString());

  // reverse the string
  StringBuilder reverseStr = str.reverse();

  // print string
  System.out.println("Reverse String = "
      + reverseStr.toString());

  // Append ', '(44) to the String
  str.appendCodePoint(44);

  // Print the modified String
  System.out.println("Modified StringBuilder = "
      + str);

  // get capacity
  int capacity = str.capacity();

  // print the result
  System.out.println("StringBuilder = " + str);
  System.out.println("Capacity of StringBuilder = "
      + capacity);
 }
}
Output:
String = Coderzpy.com
Reverse String = moc.ypzredoC
Modified StringBuilder = moc.ypzredoC,
StringBuilder = moc.ypzredoC,
Capacity of StringBuilder = 28

Note: also read about the Java StringBuffer class

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 days ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago