Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
jhtp4_10

jhtp4_10

Ratings: (0)|Views: 367 |Likes:
Published by api-3836022

More info:

Published by: api-3836022 on Oct 19, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/09/2014

pdf

text

original

10
\u00a9 Copyright 1992\u20132002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
Strings and Characters
Objectives
\ue000 To be able to create and manipulate nonmodifiable
character string objects of classS tr in g.
\u2022 To be able to create and manipulate modifiable
character string objects of classS tr in gB uff er.
\u2022 To be able to create and manipulate objects of class
Character.

\u2022 To be able to use aSt ri ngT ok en iz er object to break aS tri ng object into individual components called tokens.

The chief defect of Henry King
Was chewing little bits of string.
Hilaire Belloc
Vigorous writing is concise. A sentence should contain no
unnecessary words, a paragraph no unnecessary sentences.
William Strunk, Jr.
I have made this letter longer than usual, because I lack the
time to make it short.
Blaise Pascal
The difference between the almost-right word & the right
word is really a large matter\u2014it\u2019s the difference between the
lightning bug and the lightning.
Mark Twain
Mum\u2019s the word.
Miguel de Cervantes, Don Quixote de la Mancha
jhtp4_10.fm Page 536 Saturday, July 7, 2001 9:16 AM
Chapter 10
Strings and Characters
537
\u00a9 Copyright 1992\u20132002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
10.1 Introduction

In this chapter, we introduce Java\u2019s 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. The techniques also are appropriate for devel- oping text editors, word processors, page-layout software, computerized typesetting sys- tems and other kinds of text-processing software. We have already presented several string- processing capabilities in the text. This chapter discusses in detail the capabilities of class

String, class StringBuffer and class Character from thej av a. l an g package
and classS tr i ng T ok en i ze r from thej av a .u t il package. These classes provide the
foundation for string and character manipulation in Java.
Outline
10.1 Introduction
10.2 Fundamentals of Characters and Strings
10.3
StringCo ns tr uct or s
10.4
StringM et ho ds length, charAta nd getChars
10.5 ComparingSt ri n gs
10.6
StringMet ho d hashCode
10.7 Locating Characters and Substrings inSt ri n gs
10.8 Extracting Substrings fromSt r in gs
10.9 ConcatenatingSt r in gs
10.10 MiscellaneousS tr i ng Methods
10.11 UsingS t ri n g Methodv a lu e Of

10.12String Methodi nt e rn
10.13StringBuffer Class
10.14StringBuffer Constructors
10.15StringBuffer Methodsl e ng t h,c a pa ci t y,s e tL e ng t h and

ensureCapacity
10.16StringBuffer Methodsc h ar A t,s e tC ha r At,g et C ha r s and
reverse

10.17StringBuffer append Methods
10.18StringBuffer Insertion and Deletion Methods
10.19Character Class Examples

10.20 ClassS tr i ng T ok e ni z er
10.21 Card Shuffling and Dealing Simulation
10.22 (Optional Case Study) Thinking About Objects: Event Handling

Summary\u2022 Terminology\u2022 Self-Review Exercises\u2022 Answers to Self-Review Exercises\u2022 Exercises\u2022 Special Section: Advanced String Manipulation Exercises\u2022 Special Section: Challenging String Manipulation Projects

jhtp4_10.fm Page 537 Saturday, July 7, 2001 9:16 AM
538
Strings and Characters
Chapter 10
\u00a9 Copyright 1992\u20132002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01
10.2 Fundamentals of Characters and Strings

Characters are the fundamental building blocks of Java source programs. Every program is composed of a sequence of characters that\u2014when grouped together meaningfully\u2014is in- terpreted by the computer as a series of instructions used to accomplish a task. A program might contain character constants. A character constant is an integer value represented as a character in single quotes. As we stated previously, the value of a character constant is the integer value of the character in the Unicode character set. For example,' z ' repre- sents the integer value ofz, and'\ n ' represents the integer value of newline. See Appen- dix D for the integer equivalents of these characters.

A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters, such as+,-,*,/,$ and others. A string is an object of classS t ri n g. String literals or string constants (often calledanon ym ousS tr i ng

objects) are written as a sequence of characters in double quotation marks as follows:
"John Q. Doe"
(a name)
"9999 Main Street"
(a street address)
"Waltham, Massachusetts"
(a city and state)
"(201) 555-1212"
(a telephone number)
AS t ri n g may be assigned in a declaration to aS t ri n g reference. The declaration
String color =" b l ue ";
initializesS t ri n g referencec o lo r to refer to the anonymousS tr i ng object"b l ue ".
Performance Tip 10.1
Java treats all anonymousS t r in gs with the same contents as one anonymousSt r i ng ob-
ject that has many references. This conserves memory.
10.1
10.3String Constructors

ClassSt r in g provides nine constructors for initializingS tr i ng objects in a variety of ways. Seven of the constructors are demonstrated in Fig. 10.1. All the constructors are used in theSt r in g Co n st r uc to r s application\u2019sm a in method.

Line 25 instantiates a newS tr i ng object and assigns it to references1, using class
String\u2019s default constructor. The new String object contains no characters (thee mpty
string) and has a length of 0.
Line 26 instantiates a newS tr i ng object and assigns it to references2, using class
String\u2019s copy constructor. The new String object contains a copy of the characters in
theSt r in g objects that is passed as an argument to the constructor.
1
// Fig. 10.1: StringConstructors.java
2
// This program demonstrates the String class constructors.
34
// Java extension packages
5
importja v a x. s w in g . *;
6
Fig. 10.1
Fig. 10.1
Fig. 10.1
Fig. 10.1 Demonstrating theS tr in g class constructors (part 1 of 2).
jhtp4_10.fm Page 538 Saturday, July 7, 2001 9:16 AM

Activity (3)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->