Home » Java Tutorial » Java Stringbuilder with Examples

Java Stringbuilder with Examples

A java StringBuilder is a class used to create mutable (modifiable) sequences of characters. However, StringBuilder has been available in java since JDK version 1.5. Java provides a string class that creates an immutable (unmodifiable) sequence of characters.

A StringBuilder java 8 is an alternative class for Java string class as it creates mutable objects. A StringBuilder class does not provide a synchronisation guarantee. For that matter, java has provided a string buffer class. The working of a StringBuilder class is almost the same as the StringBuffer class. If synchronisation is required in a project, you must need to use the StringBuffer class.

How to use StringBuilder in java?

To use the string builder class, Firstly, import the StringBuilder class from the java.lang.object package. Secondly, create an object of the StringBuilder class in your code. Now perform the operations on a string using in-built functions which are predefined in the StringBuilder class.

Constructors in java StringBuilder.

  1. StringBuilder() – Creates an empty string builder object with a starting capacity of 16 characters.
  2. StringBuilder(char sequence) – Creates string builder object with the same character string passed in the function parameter.
  3. StringBuilder(int string length) – Creates an empty string builder object with the number of characters specified in the parameter.
  4. StringBuilder(string s) – creates a string builder object with the same string stored in it which is passed to the constructor.

In-built functions in java string builder class.

  1. append(str) – String function will append the string given in the parameter to the string.
  2. append(ascii value) –  Pass ASCII value as a parameter to the function. The function will append the associated character to the string.
  3. capacity() – It will return the capacity of the object.
  4. charAt(index) – It will return the character that is present at the given index.
  5. codePointAt(index) – it will return the ASCII value of the character of the passed index.
  6.  delete(int start, int end) – This will delete the substring from start parameter to end parameter.
  7. indexOf(char/string) – This function will return the first occurrence of parameter passed
  8. insert(offset, string) – This function will add the string at a specified index.
  9. length() – It will return the length of the string.
  10. lastIndexOf(str) – This function will return the last occurrence of passed parameter.
  11. replace(int start, int end, string) – This function is used to replace some part of the original string with a specified string.
  12. reverse() – This will return the string in reverse order.
  13. substring(int index) – This function will return the substring from the given index.
  14. subsequence(int start, int end) – This will return the substring of given parameters.
  15. setLength(int length) – This will set the length of string and return the string of specified length
  16. setCharAt(int index, char ch) – This function will set the specified character at the specified index.
  17. toString() – StringBuilder to string in java will display the data string in StringBuilder class object in the form of string.

Java StringBuilder example using in-built functions.

Using all the in-built functions in one program to display an appropriate output of string.

Example

import java.lang.StringBuilder;
public class strbuilder {
    public static void main(String[] args) {
        StringBuilder str = new StringBuilder("Pradtutorials");
        System.out.println("Original string: "+str.toString()); //this will display the data string in StringBuilder class object in the form of string.
        
        System.out.println("Capacity of string: "+str.capacity()); // this will print the capacity of the object
        
        System.out.println("Character at 3rd index: "+str.charAt(3)); // This will print character d
        
        System.out.println("Unicode of character at 3rd index: "+str.codePointAt(3)); // this will print the ASCII value of character d
        
        System.out.println("Index of character tutorials: "+str.indexOf("tutorials")); // This will display starting character of tutorials substring
        
        System.out.println("Length of string: "+str.length()); // This will display length of string
        
        System.out.println("Last index of String tutorials: "+str.lastIndexOf("t")); //This print the index of last occurence of character t
        
        System.out.println("Substring at index 4: "+str.substring(4)); //This will print substring from index 4
        
        System.out.println("Substring at index 4: "+str.subSequence(0, 4)); //This will print the character string from index 0 to index 4
        
        str.setCharAt(4, 'T'); //Setting character T at index 4
        System.out.println("Setting char at 4th index:"+str);
        
        System.out.println("Replacing tutorials with TUTORIALS: "+str.replace(4, 13, "TUTORIALS")); //This will replace the substring tutorials with TUTORIALS
        
        System.out.println("After deleting substring: "+str.delete(0, 4)); //This will delete the substring from index 0 to index 4
        
        System.out.println("Inserting string at index 4: "+str.insert(0, "Hello ")); //This will insert hello string at index 0
        
        System.out.println("Appended string:"+str.append(" team")); //This will append the team string.
        
        StringBuilder reverse = str.reverse(); //Reversing the string
        System.out.println("Reversed string: "+reverse);
        
        System.out.println("Unicode appended string: "+str.appendCodePoint(69)); //This will append the E character in the string.
        
        str.reverse();
        str.setLength(4); //Setting length of string
        System.out.println("Setting length of 4: "+str);
    }
    
}

Output

Original string: Pradtutorials
Capacity of string: 29
Character at 3rd index: d
Unicode of character at 3rd index: 100
Index of character tutorials: 4
Length of string: 13
Last index of String tutorials: 6
Substring at index 4: tutorials
Substring at index 4: Prad
Setting char at 4th index:PradTutorials
Replacing tutorials with TUTORIALS: PradTUTORIALS
After deleting substring: TUTORIALS
Inserting string at index 4: Hello TUTORIALS
Appended string:Hello TUTORIALS team
Reversed string: maet SLAIROTUT olleH
Unicode appended string: maet SLAIROTUT olleHE
Setting length of 4: EHel
Do you know?
1. Bubble Sort in Java
2. Armstrong Number in Java
3. Method overloading in java
4. Prime Number Program in Java
5. Type Casting In Java
6. Ternary Operator in Java
7. Immutable Class in Java
8 . Printf Java
9. While Loop Java
10. StringBuilder java

Pin It on Pinterest