java tutorial - Java StringBuffer Classes, and Methods - java programming - learn java - java basics - java for beginners
Learn Java - Java tutorial - Java stringbuffer - Java examples - Java programs
The StringBuffer and StringBuilder classes in Java are used when there is a need to make many changes to a character string.
- Unlike strings, objects of type StringBuffer and StringBuilder can be changed again and again, leaving behind many new unused objects.
- In Java, StringBuilder was introduced starting with Java 5. The main difference between StringBuffer and StringBuilder is then , that StringBuilder methods are not thread safe (non-synchronized).
- It is recommended that you use StringBuilder whenever possible, because it is faster than StringBuffer in Java. However, if you need thread security, the best option is the StringBuffer objects.
List of methods supported by the StringBuffer class:
|1||public StringBuffer append (String s)||Updates the value of the object that the method calls. This method takes boolean, char, int, long, Strings, etc.|
|2||public StringBuffer reverse ()||This method changes the value of the StringBuffer object that calls the method.|
|3||public delete (int start, int end)||Removes a row, starting from the initial index to the final index.|
|4||public insert (int offset, int i)||This method inserts the string s into the position mentioned by the offset.|
|5||replace (int start, int end, String str)||In this method it is replaces in the characters of substring to this StringBuffer with characters in the specified string.|
A list of other methods (with the exception of set methods), which are very similar to the methods of the row class :
|1||int capacity ()||It returns the capacity of the String buffer.|
|2||char charAt (int index)||It specified sequence character, presently represented by the string buffer specified by the argument and is returned to the argument.|
|3||void ensureCapacity (int minimumCapacity)||Make sure that the buffer capacity is at atleast equivalent to the specified minimum.|
|4|| void getChars (int srcBegin, int srcEnd,|
char  dst, int dstBegin)
|The characters are copied from this string buffer to the dst character array.|
|5||int indexOf (String str)||Returns the index in the specific line of the first occurrence of the specified substring.|
|6||int indexOf (String str, int fromIndex)||Returns the index in the given line of the first occurrence of the specified substring starting at the specified index.|
|7||int lastIndexOf (String str)||It returns the index of the specific line in last occurrence to the specified substring.|
|8||int lastIndexOf (String str, int fromIndex)||Returns the index in the given line of the last occurrence of the specified substring starting at the specified index.|
|9||int length ()||Returns the length of the string buffer (number of characters).|
|10||void setCharAt (int index, char ch)||The character with the specified index of this row buffer is ch.|
|11||void setLength (int newLength)||Sets the length of the string buffer (Stringbuffer).|
|12||CharSequence subSequence(int start, int end)||Returns a new sequence of characters, which is a subsequence of this sequence.|
|13||String substring (int start)||It is return to the new string that covers a subsequence of characters presently contained in the StringBuffer. The substrings are starts at the specified index and it’s continue to the completion of the StringBuffer.|
|14||String substring (int start, int end)||It is return to the new string and it's subsequence of characters are presently contained to this StringBuffer.|
|15||String toString ()||It is interchange of string in representing the data to this buffer line|