Java Reference
In-Depth Information
This operation appends a substring of aString consisting of four characters starting at index position 3
to the StringBuffer object, buf . The capacity of buf is automatically increased by the length of the
appended substring, if necessary.
Appending Basic Types
You have a set of versions of the append() method that will enable you to append() any of the basic
types to a StringBuffer object. These will 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 will be appended as
either " true " or " false ", and for numeric types the string will be a decimal representation of the
value. For example:
StringBuffer buf = new StringBuffer("The number is ");
long number = 999;
buf.append(number);
will result in buf containing the string " The number is 999 ".
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 will contain " The number is 99912.34 " .
There is also a version of the append() method which 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
arguments: 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