Sorry, there are no posts found on this page. Feel free to contact website administrator regarding this issue.
Java StringBuilder is a mutable sequence of characters. That means, unlike String, the content of the StringBuilder can be changed after it is crated using its methods.
The StringBuilder class API is similar to that of StringBuffer, with one difference. The StringBuilder class is not synchronized. That means you will have to take care of the synchronization if your code is trying to modify the contents of the StringBuilder using multiple threads.
However, if the code is single threaded, you should prefer to use StringBuilder instead of the StringBuffer. StringBuilder gives better performance in comparison to StringBuffer due to less overhead of the synchronization.
The Java StringBuilder extends from the Object class and implements Serializable, Appendable and CharSequence interfaces. It is contained in the java.lang package. The java.lang package is automatically imported into the code, so you do not have to import any package to use the StringBuilder class.
The StringBuilder class internally manages an array to store its contents. The length of this array is called StringBuilder capacity. The capacity of the StringBuilder is automatically increased as we add more content to it. It does it by allocating a new internal array with required length.
Below given are some of the very useful methods of the StringBuilder class.
1) The overloaded append method appends the specified content to the StringBuilder object.
2) The overloaded insert method inserts the specified content to the StringBuilder object at the specified position.
3) The charAt method returns the character located at the specified index of the StringBuilder object.
4) The delete and deleteCharAt methods delete the character(s) from the StringBuilder located at the specified index position(s).
5) The capacity method returns the current capacity (i.e. the length of the internal array) of the StringBuilder, while the ensureCapacity method ensures that the capacity of the StringBuilder is at least equal to the specified capacity.
6) The indexOf method returns the index of the first occurrence of the specified substring within the StringBuilder.
7) The reverse method reverses the content of the StringBuilder.
8) The substring method returns substring from the StringBuilder content located at the specified index positions.
9) The trimToSize method tries to reduce the storage used by the StringBuilder object. It does it allocating a new array of the smaller capacity.
Below given are some of the Java StringBuilder examples which shows how to use StringBuilder in Java.