Friday, October 26, 2018

java- java.lang.StringBuilder class, methods examples with tutorials

In this blog post, We are going to learn StringBuilder class and its method tutorials with examples.
StringBuilder class methods examples

java.lang.StringBuilder class

StringBuilder class is used to manipulate the string of characters in a mutable way. It is replaced in place of  String and StringBuffer.
Some of the features of StringBuilder class in java. This class is defined in java.lang package.
Syntax
public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, Comparable<StringBuilder>, CharSequence
Import notes of StringBuilder and differences with other String classes 
  • StringBuilder and StringBuffer is a mutable class, String is immutable class StringBuilder is faster than StringBuffer in single threaded applications because it consumes less memory and faster. 
  • Objects of StringBuilder is not threaded safe multi-threaded applications, StringBuilder works on insert and appends operations on a group of characters 
  • Use this class When you are manipulating Strings in the loop

Java StringBuilder class example

Following is list StringBuilder class and its method with examples

Creating of StringBuilder Object and add the string content to it

This example explains how to create a StringBuilder class in java

  • How to create an Empty String Builder class using StringBuilder() constructor 
  • Create a StringBuilder class with initial capacity using StringBuilder(int capacity) constructor 
  • An instance of StringBuilder class with initializing String content using StringBuilder Constructor with String or CharSequence parameters
public class StringBuilderDemo {

 public static void main(String[] args) {
  // Creating empty StringBuilder instance

  StringBuilder sb = new StringBuilder();
  // Creating StringBuilder instance with initial capactiy
  StringBuilder sb1 = new StringBuilder(2);
  // Creating StringBuilder instance with initial string content
  StringBuilder sb2 = new StringBuilder("String test");
  // Creating StringBuilder instance with CharSequence content
  CharSequence cs = "testcharsequence";
  StringBuilder sb3 = new StringBuilder(cs);

 }

}

adds string content to StringBuilder 

We have many ways we can add content to this class.
Following example explains about below things
  • concatenate string content to StringBuilder using append() method 
  • Add string content to StringBuilder with an index using insert() method. 
This class provides overloaded append() and insert() method which accepts any type of data.
append() method add the string to existing string content.
insert() method inserts the string content at the specified index position with starting index position zero
public class StringBuilderDemo {

 public static void main(String[] args) {
  StringBuilder sb = new StringBuilder("Test  ");
  sb.append("Kiran");
  sb.append(" example");
  System.out.println(sb);

  StringBuilder sb1 = new StringBuilder("Welcome  ");
  sb1.insert(0,"Kiran");
  System.out.println(sb1);
 }

}
and Output is
Test  Kiran example
KiranWelcome  

Useful other StringBuilder method examples

The below example explains about

  • How to replace the string content in StringBuilder using replace() method
  • How to delete the string of part of characters from StringBuilder using delete() method 
  • how to reverse String content in StringBuilder using reverse() method 
  • capacity and ensureCapacity() method

public class StringBuilderDemo {

 public static void main(String[] args) {
  
  // relace method example
  StringBuilder sb = new StringBuilder("Welcome test example  ");
  sb.replace(4,11,"kiran");
  System.out.println(sb);
  
  //delete method example
  StringBuilder sb1 = new StringBuilder("This is demo application ");
  sb1.delete(5,11);
  System.out.println(sb1);
  
  //reverse method example
  StringBuilder sb2 = new StringBuilder("cloudhadoop");
  sb2.reverse();
  System.out.println(sb2);
  
  //capacity and ensureCapacity method example
  StringBuilder sbc = new StringBuilder();
  System.out.println(sbc.capacity()); // Default value is 16
  sbc.append("cloudhadoop website");
  sbc.ensureCapacity(1);//(current capactiy*2)+2 
  System.out.println(sbc.capacity()); 

 }

}
and Output is

Welckirant example  
This o application 
poodahduolc
16
34

Related article


EmoticonEmoticon