Java Reference
In-Depth Information
Java SE 8
As you'll see in Section 17.7, you can combine regular-expression processing with Java SE
8 lambdas and streams to implement powerful
String
-and-file processing applications.
In this chapter, you learned about more
String
methods for selecting portions of
String
s
and manipulating
String
s. You learned about the
Character
class and some of the meth-
ods it declares to handle
char
s. The chapter also discussed the capabilities of the
String-
Builder
class for creating
String
s. The end of the chapter discussed regular expressions,
which provide a powerful capability to search and match portions of
String
s that fit a par-
ticular pattern. In the next chapter, you'll learn about file processing, including how per-
sistent data is stored and and retrieved.
Summary
Section 14.2 Fundamentals of Characters and Strings
• A character literal's value (p. 597)
is its integer value in Unicode (p. 597). Strings can include
letters, digits and special characters such as
+
,
-
,
*
,
/
and
$
. A string in Java is an object of class
String
.
String
literals (p. 597) are often referred to as
String
objects and are written in a pro-
gram in double quotes.
Section 14.3 Class
String
•
String
objects are immutable (p. 599)—after they're created, their character contents cannot be
changed.
•
String
method
length
(p. 599) returns the number of characters in a
String
.
•
String
method
charAt
(p. 599)
returns the character at a specific position.
•
String
method
regionMatches
(p. 601) compares portions of two strings for equality.
•
String
method
equals
tests for equality. The method returns
true
if the contents of the
String
s
are equal,
false
otherwise. Method
equals
uses a lexicographical comparison (p. 602) for
String
s.
• When primitive-type values are compared with
==
, the result is
true
if both values are identical.
When references are compared with
==
, the result is
true
if both refer to the same object.
• Java treats all string literals with the same contents as a single
String
object.
•
String
method
equalsIgnoreCase
performs a case-insensitive string comparison.
•
String
method
compareTo
uses a lexicographical comparison and returns 0 if the
Strings
are
equal, a negative number if the string that calls
compareTo
is less than the argument
String
and
a positive number if the string that calls
compareTo
is greater than than the argument
String
.
•
String
methods
startsWith
and
endsWith
(p. 604) determine whether a string starts with or
ends with the specified characters, respectively.
•
String
method
indexOf
(p. 605)
locates the first occurrence of a character or a substring in a
string.
String
method
lastIndexOf
(p. 605) locates the last occurrence of a character or a sub-
string in a string.
•
String
method
substring
copies and returns part of an existing string object.
•
String
method
concat
(p. 608) concatenates two string objects and returns a new string object.