1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes
2 StringBuffer Class StringBuffer is an alternative to the String class In general it can be used anywhere a String can be used StringBuffer is more flexible than the String class When to use: –When the string is not changing use String class –When the string is changing or being dynamically built use StringBuffer
3 StringBuffer Constructors Constructs a string buffer with no characters and an initial capacity of 16 StringBuffer strBuffer = new StringBuffer( ); Constructs a string buffer with no characters and an initial capacity specified by the argument StringBuffer strBuffer = new StringBuffer( 20 ); Constructs a string buffer with the same character sequence as the string argument - initial capacity is 16 plus the length of the argument StringBuffer strBuffer = new StringBuffer( s );
4 StringBuffer Methods int capacity( ) returns the integer value of the current capacity of the string buffer StringBuffer reverse( ) returns the reverse of the character sequence of the string contained in the string buffer int length( ) returns the number of characters in the string buffer
5 StringBuffer Methods void setLength( int newLength ) sets the length of the string buffer and will possibily truncate the string buffer, additional capacity is null char charAt( int index ) returns the character at the specified index void setCharAt( int index, char ch ) sets the character at the specified index to the specified character
6 StringBuffer Methods StringBuffer append( ) appends the string representation of the argument to the string buffer StringBuffer insert( ) inserts the string representation of the argument into the string buffer
7 StringBuffer Example StringBuffer strBuf = new StringBuffer( ); strBuf.append( Welcome ); strBuf.append( ); strBuf.append( to ); strBuf.append( ); strBuf.append( Java ); strBuf.insert( 11, HTML and );
8 StringBuffer Notes Every string buffer has a capacity If capacity is exceeded, it is automatically made larger to accommodate additional characters When the string changes frequently, the string buffer is more efficient
9 StringTokenizer Class Used to break a string into its individual pieces Each piece is an individual word within the string Uses a set of characters as delimiters to distinguish the breaks between words Each individual word is called a token
10 StringTokenizer Constructors Constructor for a string with specific delimiters with delimiter returned with the token public StringTokenizer( String s, String delim, boolean return ) Constructor for a string with a specific delimiters and the delimiter is not returned with token public StringTokenizer( String s, String delim ) Constructor for a string with default delimiters \t\n\r and not returned with token public StringTokenizer( String s )
11 StringTokenizer Methods boolean hasMoreTokens( ) returns true if any tokens are left in the string String nextToken( ) returns the next token in the string String nextToken( String delim ) returns the next token after resetting delimiter int countTokens( ) returns the number of tokens remaining in the string
12 StringTokenizer Usage import java.util.StringTokenizer; static BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in) ); StringTokenizer st = new StringTokenizer( keyboard.readLine() ); int age = new Integer( st.nextToken( ) ).intValue;