Java Reference
In-Depth Information
This operation appends the substring of aString that starts at index position 3 and ends at index position
6, inclusive, to the StringBuffer object buf . The object buf then contains the string "Hard work" . The
capacity of buf would be automatically increased if the resultant's length exceeds the capacity.
Appending Basic Types
You have a set of versions of the append() method that enable you to append() the string equivalent of val-
ues of any of the primitive types to a StringBuffer object. These versions of append() accept arguments
of any of the following types: boolean , char , byte , short , int , long , float, or double . In each case, the
value is converted to a string equivalent of the value, which is appended to the object, so a boolean variable
is appended as either “ true ” or “ false ,” and for numeric types the string is a decimal representation of the
value. For example
StringBuffer buf = new StringBuffer("The number is ");
long number = 99L;
buf.append(number);
results in buf containing the string "The number is 99" .
There is nothing to prevent you from appending constants to a StringBuffer object. For example, if you
now execute the statement
buf.append(12.34);
the object buf contains "The number is 9912.34" .
There is also a version of the append() method that accepts an array of type char[] as an argument.
The contents of the array are appended to the StringBuffer object as a string. A further variation on this
enables you to append a subset of the elements from an array of type char[] by using two additional argu-
ments: one to specify the index of the first element to be appended, and another to specify the total number
of elements to be appended. An example of how you might use this is as follows:
char[] text = { 'i', 's', ' ', 'e', 'x', 'a', 'c', 't', 'l', 'y'};
buf.append(text, 2, 8);
Search WWH ::




Custom Search