Strings: ©the Mcgraw-Hill Companies, Inc. Permission Required For Reproduction or Display
Strings: ©the Mcgraw-Hill Companies, Inc. Permission Required For Reproduction or Display
Strings
The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 9 - 1
Objectives
After you have read and studied this chapter, you
should be able to
Write string processing program using String,
StringBuilder, and StringBuffer objects.
Differentiate the three string classes and use the correct
class for a given task.
Specify regular expressions for searching a pattern in a
string.
Use the Pattern and Matcher classes.
Compare the String objects correctly.
Chapter 9 - 2
Strings
A string is a sequence of characters that is treated
as a single value.
Instances of the String class are used to
represent strings in Java.
We can access individual characters of a string by
calling the charAt method of the String object.
Chapter 9 - 3
name
This
Thisvariable
variablerefers
referstotothe
the
whole
string.
whole string.
name.charAt( 3 )
The
Themethod
methodreturns
returnsthe
the
character
at
position
#
character at position #3.3.
Chapter 9 - 4
javaCount
= 0;
boolean
repeat
= true;
String
word;
Continue
Continuereading
readingwords
words
and
count
how
many
and count how manytimes
times
the
word
Java
occurs
in
the word Java occurs inthe
the
input,
ignoring
the
case.
input, ignoring the case.
while ( repeat ) {
Notice
Noticehow
howthe
thecomparison
comparison
isisdone.
We
are
done. We arenot
notusing
using
the
==
operator.
the == operator.
} else if ( word.equalsIgnoreCase("Java") ) {
javaCount++;
}
}
Chapter 9 - 5
Meaning
compareTo
substring
trim
valueOf
startsWith
endsWith
Chapter 9 - 6
Pattern Example
Suppose students are assigned a three-digit code:
The first digit represents the major (5 indicates computer science);
The second digit represents either in-state (1), out-of-state (2), or
foreign (3);
The third digit indicates campus housing:
On-campus dorms are numbered 1-7.
Students living off-campus are represented by the digit 8.
The 3-digit pattern to represent computer science majors living on-campus is
5[123][1-7]
first
character
is 5
second
character
is 1, 2, or 3
third
character
is any digit
between 1 and 7
Chapter 9 - 7
Regular Expressions
The pattern is called a regular expression.
Rules
Chapter 9 - 8
Description
[013]
A single digit 0, 1, or 3.
[0-9][0-9]
[0-9&&[^4567]]
[a-z0-9]
[a-zA-z][a-zA-Z09_$]*
[wb](ad|eed)
is a single digit.
Chapter 9 - 9
//assign string
modifiedText =
originalText.replaceAll("[aeiou]","@");
Chapter 9 - 10
is equivalent to
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
matcher.matches();
Chapter 9 - 11
Chapter 9 - 12
Chapter 9 - 13
Effect of Immutability
We can do this
because String
objects are
immutable.
Chapter 9 - 14
Chapter 9 - 15
StringBuffer Example
word
Changing a string
Java to Diva
word
: StringBuffer
Java
Before
: StringBuffer
Diva
After
Chapter 9 - 16
Chapter 9 - 17
Chapter 9 - 18