String Handling
StringBuffer class character class StringTokenizer class
StringBuffer class Provides much of the functionalities of Strings Represents growable and writable character
sequences May have the charcters in the middle or
appended to the end Will grow automatically Allow room for growth
StringBuffer Constructors StringBuffer()
Creates with no parameters Reserves room for 16 characters
without reallocation StringBuffer(int size)
Explicitly sets the size of the buffer
Constructor StringBuffer(String str)
Str sets the initial contents of the StringBuffer Reserves room for 16 more characters object
without reallocation Reallocation is expensive process in terms of
time Frequent reallocation can fragment memory
Methods length()
Give number of characters or length of a StringBuffer
int length() capacity()
Total allocated capacity can be found int capacity()
ensureCapacity() capacity specifies the size of the buffer To preallocate room for a certain number of
characters after a stringBuffer has been constructed
To set the size of the buffer Useful if you know in advance that you
will be appending a large number of small strings to a stringBuffer
void ensureCapacity(int capacity)
setLength() To set the length of the buffer within a
StringBuffer object If we increase the size of the buffer, null
characters are added to the end of the existing buffer
If we setLength() with a value less than the current value returned by length(), then the characters stored beyond the new length will be lost
void setLength(int len)
charAt() Extraction of single character from a stringBuffer char charAt(int where)
setCharAt() Set a specified character at the specified position void charAt(int where, char ch)
getChars() To copy a substring of a StringBuffer into an array void getChars(int sourceStart, int sourceEnd,
char target[], int targetStart)
append() To concatenate the string representation of
any other type of data to the end of the invoking StringBuffer object
StringBuffer append(String str) StringBuffer append(int num) StringBuffer append(Object obj) String.valueOf() is called for each
parameter to obtain its string representation The result is appended to the current
StringBuffer object
insert() Inserts one string into another It calls String.valueOf() to obtain the string
representation of the value it is called with This string is then inserted into the
invoking StringBuffer object StringBuffer insert(int index, String str) StringBuffer insert(int index, char ch) StringBuffer insert(int index, Object obj)
reverse() Can reverse the characters within a
StringBuffer object Return the reversed object on which it is
performed StringBuffer reverse()
replace() Replaces one set of characters with another
set inside a StringBuffer StringBuffer replace(int startIndex,
int endIndex, String str)
delete() To delete characters from the StringBuffer
object StringBuffer delete(int startIndex,
int endIndex)Deletes the sequence of characters from
the invoking object StringBuffer delete(int loc)
Deletes a single character at that specified location
Returns a StringBuffer object
substring() Returns the specified segment of the
StringBuffer String substring(int startIndex)
Returns the string from startIndex to the ned of the invoking StringBuffer object
String substring(int startIndex, int endIndex)
Returns the substring that starts at startIndex and runs thru end-1
Character class The type-wrapped class for character Most of the methods are static and take
atleast a character argument Also contains a constructor that receives a
char argument to initialize a character object Character(char ch) ch – specifies the character that will be
wrapped by the Character object
char charValue() To obtain the char value contained in
character object Returns the character
static boolean isDefined(char ch) Returns true if ch is defined by unicode.
static boolean isDigit(char ch) Returns true if ch is a digit.
static boolean isJavaIdentifierStart(char ch) Returns true if ch is allowed as the first
character of the Java identifier static boolean isJavaIdentifierPart(char ch)
Returns true if ch is allowed as the part character of the Java identifier(other than the first character)
static boolean isLetter(char ch) Returns true if ch is a letter.
static boolean isLetterOrDigit(char ch) Returns true if ch is a digit or a letter.
static boolean isLowerCase(char ch) Returns true if ch is a lower case letter
static boolean isUpperCase(char ch) Returns true if ch is a upper case letter
static boolean isSpaceCase(char ch) Returns true if ch is a space character
static char toLowerCase(char ch) Returns lowercase equivalent of ch
static char toUpperCase(char ch) Returns uppercase equivalent of ch
static char forDigit(int digit, int radix) Returns a char after converting the integer digit
into a character in the number system specified by the integer radix(base of the number)
static int digit(char ch, int radix) Returns the integer value after converting the
character ch into an integer in the number system specified by the integer radix(base of the number)
static boolean equals(Charater ch) Returns true if the contents in object ch is
equals to contents in the invoked object
StringTokenizer class Is available in java.util package That breaks a string into its component
tokens Tokens are seperated from one another using
delimiters Default delimiters – blank, tab, new line,
carriage return Other characters used as delimiters
: ; ,
Constructors StringTokenizer(String str)
str is the string that will be tokenized the default delimiters are used
StringTokenizer(String str, String delimiters) Delimiters is a string that specifies the delimiters
StringTokenizer(String str, String delimiters, boolean delimAsToken)
If delimAsToken true, then the delemiters are also returned as tokens when the string is parsed
Methods int countTokens()
Using the current set of delimiters, the method determines the number of tokens left to be parsed and returns the result
String nextToken() Returns the next token as a string
boolean hasMoreTokens() Returns true if one or tokens remain in the string
and returns false if there is none