Lab 3
Lab 3
OBJECT-ORIENTED PROGRAMMING
LAB 3: STRINGBUFFER, STRINGBUILDER, STRINGTOKENIZER
I. Objective
After completing this tutorial, you can:
Creates a StringBuffer with the passed argument as the size of the empty buffer.
Creates a StringBuffer with the passed String as the initial content of the buffer. 16 contingent memory
characters are pre-allocated, not including the buffer, for modification purposes.
StringBuffer Methods
1. length()
Returns the StringBuffer object’s length.
2. capacity()
Appends the specified argument string representation at the end of the existing String Buffer. This
method is overloaded for all the primitive data types and Object.
Removes the characters in a substring of this string buffer starting at index start and extending to the
character at index end - 1 or to the end of the string buffer if no such character exists. If start is
equal to end, no changes are made. This method may throw StringIndexOutOfBoundsException if
the value of start is negative, greater than the length of the string buffer, or greater than end.
The character at index indexof this string buffer is set to ch. This method may
throw IndexOutOfBoundsException if the value of index is negative or is greater than or equal to
the length of the string buffer.
Replaces the characters in a substring of this string buffer with characters in the specified string str.
The substring to be replaced begins at index start and extends to the character at index end - 1 or
to the end of the string buffer if no such character exists. The substring is removed from the string buffer,
and then the string str is inserted at index start. If necessary, the string buffer is lengthened to
accommodate the string str. This method may throw StringIndexOutBoundsException if the
value of start is negative, greater than the length of the string buffer, or greater than end.
• Java StringBuilder class is designed for use as a drop-in replacement for StringBuffer in
places where the string buffer was being used by a single thread (as is generally the case).
• If execution speed and performance is a factor, StringBuilder class can be used in place
of StringBuffer.
• The bread-and-butter operations provided by the StringBuilder Class are the append() and
insert() methods. These methods are overloaded within StringBuilder in order to
accommodate different data type.
• The general process flow of StringBuilder append and insert methods is: (1) converts a given
data to a string then (2) appends or inserts the characters of that string to the string builder.
Java StringBuilder append() method always adds these characters at the end of the
builder; insert() method inserts character(s) at a specified point.
StringBuilder Constructors
1. StringBuilder()
Creates an empty string builder with a default capacity of 16 (16 empty elements).
StringBuilder Methods
1. append()
StringBuilder append() method concatenates or attaches the passed String argument with the existing
declared string. It attaches it after the declared string.
StringBuilder insert() method inserts the passed String argument at the passed String index.
StringBuilder replace() method replaces the existing declared string. String replacement occurs
from the passed startingIndex up to the endingIndex.
StringBuilder delete() method deletes a character or sets of characters. Deletion occurs at passed
startingIndex up to endingIndex.
The reverse() method of StringBuilder class reverses the existing declared string. Invoking
a reverse() method on a StringBuilder object with no existing declared value
throws NullPointerException.
This constructor creates a string tokenizer for the specified string str. The tokenizer uses the default
delimiter set, which is the space character, the tab character, the newline character, the carriage-return
character, and the form feed character. Delimiter characters themselves are not treated as tokens.
This constructor creates a string tokenizer for the specified string str. All characters in
the delim string are the delimiters for separating tokens. Delimiter characters themselves are not treated
as tokens.
This constructor creates a string tokenizer for the specified string str. All characters in
the delim string are the delimiters for separating tokens. If the returnTokens flag is true, the
delimiter characters are also returned as tokens. Each delimiter is returned as a string of length 1. If the
flag is false, the delimiter characters are skipped and serve only as separators between tokens.
Returns the next token in the string. If there are no more tokens in the string, it throws the
exception NoSuchElementException.
2. hasMoreTokens()
Returns true if the string contains more tokens.
StringTokenizer st1 = new StringTokenizer("Hello guy How are you", " ");
while (st1.hasMoreTokens())
{
System.out.println(st1.nextToken());
}
// Hello
// guy
// How
// are
// you
3. countTokens()
Calculates the number of times that this tokenizer's nextToken method can be called before it generates
an exception. The current position is not advanced. This function returns the number of tokens remaining
in the string using the current delimiter set.
IV. Exercises
1. Write a Java program:
o Split the full name into first name and last name (using StringTokenizer).
Screen:
I 2
live 1
in 1
Ho 1
Chi 1
Minh 1
city 1
study 1
at 1
TDTU 1