Home > Tutorial > Java StringBuilder Sample Code

Java StringBuilder Sample Code

A StringBuilder Class is like a String but has a few advantages over a standard String. The big difference is that a String object is immutable and a StringBuilder object is mutable. Mutable in terms of Java means the object can’t be changed once it is created. A mutable object, on the other hand, can be modified after it is created.

The most common and used methods of the StringBuilder class are included below with sample code near the bottom. When you create an empty StringBuilder object, the capacity of the object will always be set to 16 characters. When you create a StringBuilder object that is not empty, the capacity will be set to the length of the object plus 16 more characters.

Create Empty StringBuilder Object

Creates an empty StringBuilder Object with an initial capacity of 16 characters.

StringBuilder stringBuilder = new StringBuilder();


Check StringBuilder Object Capacity

You can check the capacity of the StringBuilder object by calling the capacity() method.

stringBuilder.capacity()


Append String StringBuilder Object

Let’s add 13 characters (length of “JAVACODEDEPOT”) to the beginning of stringBuilder object.

stringBuilder.append("JAVACODEDEPOT");


Append Single Character StringBuilder Object

Let’s add a single character ‘.’ to the end of the object.

stringBuilder.append(".");


Create StringBuilder Object With String

Creates a StringBuilder Object initialized to the contents of myString.

String myString = "JAVACODEDEPOT";
StringBuilder stringBuilderTwo = new StringBuilder(myString);


Reverse StringBuilder Characters

The reverse() method will reverse the order of the characters.

stringBuilderOne.reverse();


Delete First Character of StringBuilder

Delete or remove the first character of the StringBuilder object.

stringBuilderOne.delete(0,1);


Delete First Character of StringBuilder

Append a character “J” to the beginning of the StringBuilder object.

stringBuilderOne.insert(0, 'J');


Append Characters to End of StringBuilder

Append a sequence of characters “.COM” to the end of the StringBuilder object.

stringBuilderOne.append(".COM");


Java StringBuilder Source Code:

Append a sequence of characters “.COM” to the end of the StringBuilder object.

package com.jcd.java.tutorials;

public class JavaStringBuilder 
{

	public static void main(String[] args) 
	{
		
		//Create a String Object
		String myString = "JAVACODEDEPOT.COM";
		
		//Create a new StringBuilder Object that takes a String
		StringBuilder stringBuilderTwo = new StringBuilder(myString);
		
		//Capacity of stringBuilderTwo
		System.out.println("Capacity of stringBuilderTwo: " +stringBuilderTwo.capacity());
		
		//Create a new StringBuilder Object
		StringBuilder stringBuilderOne = new StringBuilder();
		
		//Capacity of stringBuilderOne initially
		System.out.println("Capacity of stringBuilderOne: " +stringBuilderOne.capacity());
		
		//Now add 13 characters to the beginning of stringBuilder object
		stringBuilderOne.append("JAVACODEDEPOT");
		System.out.println("stringBuilderOne: " +stringBuilderOne);
				
		//Reverse stringBuilderOne sequence
		stringBuilderOne.reverse();
		System.out.println("stringBuilderOne Reversed: " +stringBuilderOne);
		
		//Reverse stringBuilderOne back to original sequence
		stringBuilderOne.reverse();
		System.out.println("stringBuilderOne Reversed: " +stringBuilderOne);
		
		//Get the length of stringBuilderone object
		System.out.println("stringBuilderOne Length: " +stringBuilderOne.length());
		
		//Delete the first character "J"
		stringBuilderOne.delete(0,1);
		System.out.println("stringBuilderOne: " +stringBuilderOne);
		
		//Add 'J' back to beginning of object
		stringBuilderOne.insert(0, 'J');
		System.out.println("stringBuilderOne: " +stringBuilderOne);
		
		//Add ".COM" to the end of the object
		stringBuilderOne.append(".COM");
		System.out.println("stringBuilderOne: " +stringBuilderOne);
				
		//Create a new String Object from StringBuilder Object
		String newString = stringBuilderOne.toString();
		System.out.println("newString: "+newString);

	}

}


Java StringBuilder Code Output:

Append a sequence of characters “.COM” to the end of the StringBuilder object.

Capacity of stringBuilderTwo: 33
Capacity of stringBuilderOne: 16
stringBuilderOne: JAVACODEDEPOT
stringBuilderOne Reversed: TOPEDEDOCAVAJ
stringBuilderOne Reversed: JAVACODEDEPOT
stringBuilderOne Length: 13
stringBuilderOne: AVACODEDEPOT
stringBuilderOne: JAVACODEDEPOT
stringBuilderOne: JAVACODEDEPOT.COM
newString: JAVACODEDEPOT.COM



References:

Smedley
Posted by

Smedley

Smedley has been doing software development for years, which mainly docues on C and C++. In the past number of years, he has focused on Java programming.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot