Java StringBuilder

Java StringBuilder

Java StringBuilder

StringBuilder class is same as StringBuffer class but there is only one difference which is synchronization. Java StringBuilder is not synchronized. In this tutorial, we will discuss about StringBuilder class in java, some great examples and exercise.


Java StringBuilder



What is StringBuilder

StringBuilder class is used to create mutable string objects in java. The StringBuffer and StringBuilder classes are same in nature with only one different of synchronization. (Synchronization makes sure that at a time only one thread is accessing your resource).

It is better to use StringBuilder class where lots of concatenation is being performed at run time. Or you need to store really long String.

It is recommended to use StringBuilder instead of StringBuffer class. Because synchronization makes processing a little bit slow. 

Let’s discuss some important points about StringBuilder


  1. StringBuilder is used to create mutable string objects which can be modified at run time.
  2. StringBuilder class is not synchronized.
  3. You can perform different operations on StringBuilder object like append, reverse, replace etc. No new object will be created at run time after modification to the original object.


StringBuilder constructors

StringBuilder have some overloaded constructors which are used to initialize a string. Let’s see them.

StringBuilder()
Creates an empty StringBuilder object with initial capacity of 16 characters. 
StringBuilder(CharSequence seq)
Creates a StringBuilder object as per specified CharSequence.
StringBuilder(int capacity)
It creates a StringBuilder object with empty String in it. But with the help of this constructor, you can specify the initial capacity.
StringBuilder(String str)
Creates a StringBuilder object as per specified String object.


StringBuilder methods 

StringBuffer and StringBuilder are just same, there is no difference between their methods as well. The only difference is of synchronization. So let’s take a look on it’s methods.

StringBuilder append(String str)
This method will append the given string at the end of the existing StringBuilder object.
int capacity()
Returns the current capacity.
char charAt(int index)
Returns the char value in this sequence at the specified index.
StringBuilder delete(int start, int end)
You can delete the characters from StringBuilder object at specified position.
StringBuilder deleteCharAt(int index)
Removes the char at the specified position in this sequence.
StringBuilder insert(int offset, String str)
This method will insert given string into the existing StringBuilder object at the given location which is the offset.
StringBuilder replace(int start, int end, String str)
This method will replace the given string at specified position in StringBuilder object and no new object will be created after replace.
StringBuilder reverse()
Method returns the reversed characters of StringBuilder object.
void setCharAt(int index, char ch)
You can set character at specified position.
String substring(int start)
Returns sub string from the specified position in StringBuilder object.
String substring(int start, int end)
You can also specify the position from where you want sub string.
String toString()
Returns a string representing the data in this sequence.
void trimToSize()
Attempts to reduce storage used for the character sequence.


1. An example on StringBuilder replace method

public class StringBuilderReplace {
 
 public static void main(String[] args) {
 
 StringBuilder strB = new StringBuilder("Welcome to StringBuilder example.");
 System.out.println("Original Text : " + strB);
 
 strB.replace(0,6,"Hello");
 System.out.println("Replaced Text : " + strB);
 
 }
}

2. An example on StringBuilder reverse method

public class StringBuilderReplace {
 
 public static void main(String[] args) {
 
 StringBuilder strB = new StringBuilder("Welcome to StringBuilder example.");
 System.out.println("Original Text : " + strB);
 
 strB.reverse();
 System.out.println("Reversed Text : " + strB);
 
 }
}

3. StringBuilder delete example

public class StringBuilderDelete {
 
 public static void main(String[] args) {

 StringBuilder sb1 = new StringBuilder("Welcome to the example of StringBuilder.");
 sb1.delete(0,6);
 System.out.println(sb1);
 
 StringBuilder sb2 = new StringBuilder("Hello");
 System.out.println(sb2);
 sb2.delete(0, sb2.length());
 System.out.println(sb2);

 StringBuilder sb3 = new StringBuilder("Welcome");
 sb3.deleteCharAt(0);
 System.out.println(sb3);
 } 
}


It’s time for some exercise

  1. Write a java program to replace input string in a given string buffer.
  2. Write a java program to find how many times an input string is found in a given string.
  3. Write a java program to reverse a string only from specified position. Example, “Hello world” reverse(0, 2) output “leHlo world”.
  4. Write a java program to remove all spaces from a given string. ” hello to java” to “hellotojava”.
  5. Write a java program to convert given character to upper case in string buffer object. “hello world”  given character is ‘l’ then “heLLo worLd”.
  6. Write a java program to find the frequency of a given character. How many times a particular character appears in string.
  7. Write a program to remove all duplicate characters from a given string.
  8. Write a program to check if the given string and it’s reverse version are same. Like “nitin” and it’s reverse version “nitin” both are same.

Related Post

Leave a Reply

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