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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago