2. 16
About Strings
String is a class, not
an intrinsic type.
Escape sequences:
Backslash ()
followed by a
character
Embedded into
strings to affect the
display
3. 16
Working with Strings
Concatenation
“Add” two strings together using +
operator
Use the += operator with String objects
Examining Strings
length() returns number of characters
charAt() returns character at specific
index
4. 16
Locating Characters in Strings
Locating Characters:
indexOf() method returns index of a
specific character.
Overloaded to return index of a character
after a certain starting index
substring() method extracts characters
from a string.
5. 16
Comparing Strings
Strings can be compared to see if they
are equal:
equals() method is case sensitive
equalsIgnoreCase() method ignores
case
6. 16
The StringBuffer Class
Java strings are immutable. Use
StringBuffer object when you need
to modify a string of characters.
Includes methods to:
Set character at specific index: setCharAt()
Append characters: append()
Insert characters at a specific index: insert()
Reverse the characters: reverse()
7. 16
Reading Files
The stream object represents bytes
moving from source to destination.
InputStreamReader works with
individual characters.
BufferedReader works with bytes or
lines of character data at a time.
8. 16
Creating Files forReading
and Writing
Use FileInputStream class for reading
from files
Use FileWriter class for writing to files
(uses internal FileOutputStream object)
9. 16
Parsing and Tokens
StringTokenizer class breaks a
string into tokens.
Uses either default rules or user-provided
delimiters to determine tokens
Use hasMoreTokens() to determine if
StringTokenizer contains more tokens
Use nextToken() to return next token
10. 16
Using StreamTokenizer
StreamTokenizer class is more flexible
than StringTokenizer.
Allows you to establish table of “rules”
Uses flags to establish various states to use with
table of rules
Commonly used constants:
TT_EOF indicates the end of stream
TT_EOL indicates the end of line
TT_NUMBER indicates a number token has been read
TT_WORD indicates a word token has been read