Java Reference
In-Depth Information
14.1
Introduction
14.2
Fundamentals of Characters and Strings
14.3
Class
String
14.3.1
String
Constructors
14.3.2
String
Methods
length
,
charAt
and
getChars
14.3.3 Comparing Strings
14.3.4 Locating Characters and Substrings in
Strings
14.3.5 Extracting Substrings from Strings
14.3.6 Concatenating Strings
14.3.7 Miscellaneous
String
Methods
14.3.8
String
Method
valueOf
14.4
Class
StringBuilder
14.4.1
StringBuilder
Constructors
14.4.2
StringBuilder
Methods
length
,
capacity
,
setLength
and
ensureCapacity
14.4.3
StringBuilder
Methods
charAt
,
setCharAt
,
getChars
and
reverse
14.4.4
StringBuilder
append
Methods
14.4.5
StringBuilder
Insertion and
Deletion Methods
14.5
Class
Character
14.6
Tokenizing
String
s
14.7
Regular Expressions, Class
Pattern
and Class
Matcher
14.8
Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises |
Special Section: Advanced String-Manipulation Exercises |
Special Section: Challenging String-Manipulation Projects | Making a Difference
This chapter introduces Java's string- and character-processing capabilities. The tech-
niques discussed here are appropriate for validating program input, displaying information
to users and other text-based manipulations. They're also appropriate for developing text
editors, word processors, page-layout software, computerized typesetting systems and oth-
er kinds of text-processing software. We've presented several string-processing capabilities
in earlier chapters. This chapter discusses in detail the capabilities of classes
String
,
StringBuilder
and
Character
from the
java.lang
package—these classes provide the
foundation for string and character manipulation in Java.
The chapter also discusses regular expressions that provide applications with the capa-
bility to validate input. The functionality is located in the
String
class along with classes
Matcher
and
Pattern
located in the
java.util.regex
package.
Characters are the fundamental building blocks of Java source programs. Every program
is composed of a sequence of characters that—when grouped together meaningfully—are
interpreted by the Java compiler as a series of instructions used to accomplish a task. A pro-
gram may contain
character literals
.
A character literal is an integer value represented as a
character in single quotes. For example,
'z'
represents the integer value of
z
, and
'\t'
rep-
resents the integer value of a tab character. The value of a character literal is the integer
value of the character in the
Unicode character set
. Appendix B presents the integer equiv-
alents of the characters in the ASCII character set, which is a subset of Unicode (discussed
in online Appendix H).
Recall from Section 2.2 that a string is a sequence of characters treated as a single unit.
A string may include letters, digits and various
special characters
,
such as
+
,
-
,
*
,
/
and
$
.
A string is an object of class
String
.
String literals
(stored in memory as
String
objects)
are written as a sequence of characters in double quotation marks, as in: