StringBuilder in Java Programming
String in Java
In this lesson, we will understand what is StringBuilder in Java Programming along with some examples.
What is StringBuilder in Java
A StringBuilder is a class in java which is used to store a mutable sequence of characters that can be modified. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread.
Syntax of Creating a StringBuilder Object
StringBuilder object_name=new StringBuilder("string value");
Example
StringBuilder sb=new StringBuilder("Dremendo");
Here we have declared a StringBuilder object sb and initialize it with the string value Dremendo.
Methods of StringBuilder Class
StringBuilder methods are useful functions used to perform a specific task on the content of the StringBuilder object. Below we have discussed all the important StringBuilder methods available in the StringBuilder class.
length() Method
The length() method returns the total number of characters (including space) present in the string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s;
int l;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
l=sb.length();
System.out.println("Length="+l);
}
}
Output
Enter a string Hello World Length=11
charAt() Method
The charAt() method returns the character at the specified index. In a string builder object, the first character starts from index 0.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s;
int l;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
System.out.println("Character at index 3="+sb.charAt(3));
}
}
Output
Enter a string Dremendo Character at index 3=m
setCharAt() Method
The setCharAt() method set a specified character at the specified index in the string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
sb.setCharAt(2,'m');
System.out.println(sb);
}
}
Output
Enter a string Tiger Timer
deleteCharAt() Method
The deleteCharAt() method removes a character at the specified index in the string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
sb.deleteCharAt(2);
System.out.println(sb);
}
}
Output
Enter a string Tiger Tier
delete() Method
The delete() method removes character sequences from the specified start index till end index-1 in a string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
sb.delete(1,5);
System.out.println(sb);
}
}
Output
Enter a string Computer Cter
indexOf() Method
The indexOf() method returns the index of the first occurrence of the specified character or string within the current string builder object. The indexOf() method accepts two arguments. The first argument is the string or character and the second optional argument is the index from where we want to check the occurrence. If the index is not given, it will start checking the occurrence from index 0.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 strings");
s1=sc.nextLine();
s2=sc.nextLine();
StringBuilder sb=new StringBuilder(s1);
System.out.println("The 2nd string first occur at index " + sb.indexOf(s2) + " in the string builder object.");
System.out.println("After index 5, the 2nd string first occur at index " + sb.indexOf(s2,5) + " in the string builder object.");
}
}
Output
Enter 2 strings I am a boy and I love java programming. I The 2nd string first occur at index 0 in the string builder object. After index 5, the 2nd string first occur at index 15 in the string builder object.
lastIndexOf() Method
The lastIndexOf() method returns the index of the last occurrence of the specified character or string within the current string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 strings");
s1=sc.nextLine();
s2=sc.nextLine();
StringBuilder sb=new StringBuilder(s1);
System.out.println("The 2nd string last occur at index " + sb.lastIndexOf(s2) + " in the string builder object.");
}
}
Output
Enter 2 strings I am a boy and I love playing football. I The 2nd string last occur at index 15 in the string builder object.
toString() Method
The toString() method returns a string representing the data of the string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s1=sc.nextLine();
StringBuilder sb=new StringBuilder(s1);
s2=sb.toString();
System.out.println(s2);
}
}
Output
Enter a string Java Programming Java Programming
append() Method
The append() method appends the specified string to the end of the current string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 strings");
s1=sc.nextLine();
s2=sc.nextLine();
StringBuilder sb=new StringBuilder(s1);
sb.append(s2);
System.out.println(sb);
}
}
Output
Enter 2 strings good bye goodbye
replace() Method
The replace() method replaces a substring with the specified string in the string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 strings");
s1=sc.nextLine();
s2=sc.nextLine();
StringBuilder sb=new StringBuilder(s1);
sb.replace(7,13,s2);
System.out.println(sb);
}
}
Output
Enter 2 strings I Love Orange Apple I Love Apple
insert() Method
The insert() method insert a specified string in the string builder object at the specified index.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 strings");
s1=sc.nextLine();
s2=sc.nextLine();
StringBuilder sb=new StringBuilder(s1);
sb.insert(0,s2);
System.out.println(sb);
}
}
Output
Enter 2 strings how are you Hello Hello how are you
reverse() Method
The reverse() method replaces the character sequence of the string builder object by the reverse of the sequence.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
sb.reverse();
System.out.println(sb);
}
}
Output
Enter a string Apple elppA
substring() Method
The substring() method returns a new string which is a substring currently contained in the string builder object.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
s=sc.nextLine();
StringBuilder sb=new StringBuilder(s);
System.out.println("Print the 1st 3 characters of the current string");
System.out.println(sb.substring(0,3));
System.out.println("Print the 1st 3 characters starting from index 2 and stopping at index 5");
System.out.println("The stopping index is not included in the substring");
System.out.println(sb.substring(2,5));
System.out.println("Print all the characters starting from index 3 till end");
System.out.println(sb.substring(3));
}
}
Output
Enter a string Dremendo Print the 1st 3 characters of the current string Dre Print the 1st 3 characters starting from index 2 and stopping at index 5 The stopping index is not included in the substring eme Print all the characters starting from index 3 till end mendo
How to compare the values of two string builder objects?
To compare the values of two string builder object, first, we have to convert the values into the string and then apply the compareTo() or compareToIgnoreCase() function of the String class on it. Let's see the example given below.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 strings");
StringBuilder sb1=new StringBuilder(sc.nextLine());
StringBuilder sb2=new StringBuilder(sc.nextLine());
if(sb1.toString().compareTo(sb2.toString())==0)
{
System.out.println("Both the strings are same");
}
else
{
System.out.println("Both the strings are not same");
}
}
}
Output
Enter 2 strings Computer Computer Both the strings are same
How to overwrite a string builder object with a new string?
To overwrite a string builder object with a new string, we have to replace the old string with the new one. Let's see the example given below.
Example
import java.util.Scanner;
public class StringBuilderMethod
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter 2 strings");
StringBuilder sb=new StringBuilder(sc.nextLine());
s=sc.nextLine();
System.out.println("Current string in string builder object = " + sb);
sb.replace(0,sb.length(),s);
System.out.println("New string in string builder object = " + sb);
}
}
Output
Enter 2 strings Apple Ball Current string in string builder object = Apple New string in string builder object = Ball