PREV NEXT
What is StringBuilder? (Difference between a StringBuffer and StringBuilder)
- StringBuffer is synchronized (which means that, even when multiple threads act on the StringBuilder object, they would be executed one after the other providing us reliable results) whereas StringBuilder is not.
- StringBuilder executes faster than StringBuffer since it does not take the time for synchronization.
- Since StringBuilder is not synchronized, it may give erroneous results in multithread environment whereas StringBuffer gives reliable results in these cases.
- StringBuilder class is introduced in jdk 1.5 version.
- Both StringBuffer and StringBuilder objects are mutable unlike String object which is unmutable.
- Methods of StringBuilder class are similar to those of StringBuffer class.
Creating StringBuilder objects:
StringBuilder can be defined simply by the use of new operator and by passing the string value inside the StringBuilder constructor.
Syntax:
StringBuilder <object> = new StringBuilder(String_value) |
Example:
StringBuilder sb = new StringBuilder(“Hello”);
The other way is to create an empty object of StringBuilder and then adding the value using append or insert method.
Example:
StringBuilder sb1 = new StringBuilder();
sb1.append(“hi”);
sb1.insert(0, “hello”); //inserting hello from the 0th position in sb1.
We can also mention the size of the StringBuilder while creating the StringBuilder object.
Example:
StringBuilder sb1 = new StringBuilder(100);
This indicates that we can add 100 characters in sb1 which can also be modified later since the size of the StringBuilder can be changed as per the need.
Inserting and appending a String in StringBuilder:
Before inserting/appending a string, an empty StringBuilder object is created.
Syntax for insert():
StringBuilder insert(int p, x); // inserts x in the position p |
Syntax for append():
StringBuilder append (x1); // appends x1 to the StringBuilder |
Example:
StringBuilder name= new StringBuilder();
names.insert(0,“Anjali”);
names.append(“Bissa”);
Deleting characters from the StringBuilder:
Deletion and insertion are similar.
Syntax:
StringBuilder delete(int p1, int p2) |
This would delete the characters starting from the position p1 till p2-1.
Example:
StringBuilder name= new StringBuilder();
names.insert(0,“Anjali”);
names.append(“Bissa”);
names.delete(6,11); // deletes Bissa from name, name would contain Anjali
Reversing a string using StringBuilder:
Reversing a string is as simple as using reverse() method with the StringBuilder object.
Syntax:
StringBuilder reverse() |
Example:
StringBuilder name= new StringBuilder(“madam”);
StringBuilder Reversed=name.reverse(); // Reversed would also have “madam”
The method toString() can be used to convert the StringBuilder object again to String object.
Finding the Length of the String using StringBuilder:
Finding the length of the StringBuilder is simple.
Syntax:
int length() |
Example:
StringBuilder name= new StringBuilder(“Hello”);
int l = name.length(); // l would contain 5
Replacing characters in the string using stringBuilder:
Sequence of Characters can be replaced with a new set of characters using the replace() method.
Syntax:
StringBuilder replace(int i, int j, String s) |
Replaces the characters starting from ith position till the j-1th position with the string s.
Example:
StringBuilder sb = new StringBuilder(“preorder”);
sb.replace(0,3,”post”);
Finding Substring from the string using StringBuilder:
Syntax:
StringBuilder substring(int i); |
Retrieves the characters starting from the position i from the StringBuilder object.
Example:
StringBuilder sb = new StringBuilder(“BlackBoard”);
String s = sb.substring(5); // contains the substring Board
Syntax:
StringBuilder substring(int i, int j) |
Retrieves the characters starting from the position i till the j-1th position.
Example: StringBuilder sb = new StringBuilder(“BlackBoard”);
String s = sb.substring(0,5); //contains the substring Black
Finding the index of a character in StringBuilder object:
Position of a character(s) can be found using indexOf() method. Returns the first occurrence of the substring from left to right.
Syntax:
int indexOf(String str) |
Example:
StringBuilder sb = new StringBuilder(“book”);
int i = sb.indexOf(“ok”); // i contains 2
Similarly, last occurrence of the substring can also be found using lastIndexOf() method.
Syntax:
int lastIndexOf(String st) |
Returns the last occurrence of the substring in the StringBuilder object. In other words, it finds the first occurrence of the substring from right to left.
Example:
StringBuilder sb = new StringBuilder(“This is an amazing book”);
int index = sb.lastIndexOf(“is”); //index would contain 5
StringBuilder Methods |
StringBuilder Examples
|
StringBuilder append(x)
Where x can be of any datatype. It will be added to the StringBuilder object |
StringBuilder sb1 = new StringBuilder(“fellow”);
sb1.append(“ship”); //sb1 will contain fellowship |
StringBuilder insert(int position, x)
Inserts the data x in the StringBuilder object starting from the given position. |
StringBuilder sb1 = new StringBuilder(“Black”);
sb1.insert(6, “Board”) //sb1 will contain Black Board |
StringBuilder delete(int start, int end) Deletes the data x in the StringBuilder object starting from the given position. |
StringBuilder sb1 = new StringBuilder(“Black Board”);
sb1.delete(6, 10); //sb1 will contain Black |
StringBuilder reverse()
Reverses the character sequence present in the string. |
StringBuilder sb1 = new StringBuilder(“fellow”);
sb1.reverse(); //sb1 will contain wollef |
StringBuilder replace(int start, int end, String st)
Replaces the characters (in the StringBuilder object) from the start position to the end-1 position by the characters given in str. |
StringBuilder sb1 = new StringBuilder(“fellow”);
sb1.replace(0,2,”ye”) //sb1 will contain yellow |
StringBuilder substring(int i)
Retrieves the string from the StringBuilder object starting from the ith position till the end. StringBuilder substring(int i, int j) Retrieves the string from the StringBuilder object starting from the ith position till the j-1th position. |
StringBuilder sb1 = new StringBuilder(“fellow”);
sb1.substring(3); //sb1 will contain low
StringBuilder sb1 = new StringBuilder(“fellow”); sb1.substring(3,6); //sb1 will also contain low |
int length()
Returns the length of the StringBuilder object. |
StringBuilder sb1 = new StringBuilder(“fellow”);
Int length = sb1.length(); //length will have 6 |
int indexOf(String str)
Returns the First Occurrence of the substring str in the StringBuilder object (from left to right) |
StringBuilder sb1 = new StringBuilder(“fellow”);Int low = sb1.indexOf(“low”);
//sb1 will have 3
|
int LastIndexOf(String str)
Returns the Last Occurrence of the substring str in the StringBuilder object (from left to right) I.e. the first occurrence from the right. |
StringBuilder sb1 = new StringBuilder(“He is part of this”);Int low = sb1.LastIndexOf(“is”);
//sb1 will have 17 |