StringBuffer is a peer class of String class. While String creates strings of fixed length, StringBuffer creates strings of flexible length that can be modified in terms of both length and content. We can insert characters into or delete characters from a string. The few differences between String and StringBuffer are listed below.
· StringBuffer objects are mutable.
· It provides functions like setCharAt(), deleteCharAt() and etc.
· It allocates extra 16 bits of memory
· It can be treated as a dynamic string.
StringBuffer can be initialized as follows
StringBuffer s1 = new StringBuffer(“abcd”)
append() | used for the concatenate the string in string buffer. This is better to use for dynamic string concatenation. |
insert() | used to insert any string or character at the specified position in the given string. |
reverse() | used to reverse the string present in string buffer. |
setCharAt() | used to set the specified character in buffered string at the specified position. |
charAt() | used to get the character at the specified position of the given string. |
substring() | used to get the sub string from the buffered string |
deleteCharAt() | used to delete the specific character at a given position from the buffered string |
length() | used to finding the length of the buffered string. |
delete() | used to delete multiple character at once from n position to m. |
capacity() | used to know the capacity of the object i.e. Number of characters + 16. |
0 comments:
Post a Comment