img
Method
Description
String[ ] split(String regExp)
Decomposes the invoking string into par ts and returns an array
that contains the result. Each par t is delimited by the regular
expression passed in regExp.
String[ ] split(String regExp, int max)
Decomposes the invoking string into par ts and returns an array
that contains the result. Each par t is delimited by the regular
expression passed in regExp. The number of pieces is specified
by max. If max is negative, then the invoking string is fully
decomposed. Other wise, if max contains a nonzero value,
the last entr y in the returned array contains the remainder
of the invoking string. If max is zero, the invoking string is
fully decomposed.
Returns a substring of the invoking string, beginning at star tIndex
CharSequence
subSequence(int star tIndex,
and stopping at stopIndex. This method is required by the
int stopIndex)
CharSequence inter face, which is now implemented by String.
Notice that several of these methods work with regular expressions. Regular expressions
are described in Chapter 27.
StringBuffer
StringBuffer is a peer class of String that provides much of the functionality of strings.
As you know, String represents fixed-length, immutable character sequences. In contrast,
StringBuffer represents growable and writeable character sequences. StringBuffer may have
characters and substrings inserted in the middle or appended to the end. StringBuffer will
automatically grow to make room for such additions and often has more characters preallocated
than are actually needed, to allow room for growth. Java uses both classes heavily, but many
programmers deal only with String and let Java manipulate StringBuffers behind the scenes
by using the overloaded + operator.
StringBuffer Constructors
StringBuffer defines these four constructors:
StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
The default constructor (the one with no parameters) reserves room for 16 characters
without reallocation. The second version accepts an integer argument that explicitly sets the
size of the buffer. The third version accepts a String argument that sets the initial contents
of the StringBuffer object and reserves room for 16 more characters without reallocation.
StringBuffer allocates room for 16 additional characters when no specific buffer length is
requested, because reallocation is a costly process in terms of time. Also, frequent reallocations
can fragment memory. By allocating room for a few extra characters, StringBuffer reduces the
number of reallocations that take place. The fourth constructor creates an object that contains
the character sequence contained in chars.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home