Title: Java
1- Java
- How to Program, 2e
- Deitel Deitel
- Chapter 8
- Strings and Characters
- Slides by Sylvia Sorkin, Community College of
Baltimore County - Essex Campus
2Chapter 8 Topics
- String constructors
- String methods length, charAt, getChars
- Comparing Strings
- Locating characters and substrings
- Extracting substrings
- Concatenating Strings
- StringBuffer class, constructors, methods
- Wrapper class Character
- StringTokenizer class
3In Java a String is an object
String OBJECT
set of instance variables set of methods
for manipulating strings
4String references can be variables
String message new String( hello )
String lastName lastName Deitel
String friend Deitel
String OBJECT
hello
message
Operations
5String references can be initialized to null
String message new String( hello )
String lastName // no new
String object is created lastName Deitel
String friend Deitel
String OBJECT
hello
null lastName
message
Operations
6String references can be literals
String message new String( hello )
String lastName lastName Deitel
// String object is created String friend
Deitel
String OBJECT
String OBJECT
hello
null lastName
Deitel
message
Operations
Operations
7String references can be variables or literals
String message new String( hello )
String lastName lastName Deitel
String friend Deitel
String OBJECT
String OBJECT
hello
lastName
Deitel
message
Operations
Operations
8String references can be variables or literals
String message new String( hello )
String lastName lastName Deitel
String friend Deitel // no new
String object is created
String OBJECT
String OBJECT
hello
lastName
Deitel
message
Operations
Operations
friend
9class String constructors
// Constructors public String( ) //
Constructs a new empty String public String(
String s ) // Constructs a new copy of String
s public String( char charArray ) //
Constructs a new String from the character array
10Some String methods
String OBJECT
Data
Operations length( ) charAt(int
x) indexOf(char ch) equals(String s)
11String indexes start with 0
- The first character of a string is always at
position 0.
String s1 new String( today )
Indexes 0 1 2 3 4 t o d a y
12String indexes start with 0
- The first character of a string is always at
position 0.
String message new String( Good Bye! )
Indexes 0 1 2 3 4 5 6 7 8 G o o d B
y e !
13How many String objects are created?
String s1 new String( today ) String
s2 today String s3 new String(
today ) String s4 new String ( Today
)
14Using String methods
String s1 new String( today ) String
s2 today String s3 new String(
today ) String s4 new String ( Today
)
String OBJECT
String OBJECT
s3
today
today
s1
String OBJECT
Operations
Operations
Today
s2
s4
Operations
15Using operator
- When comparing values of primitive data types
with , the result is true if both values are
identical. - When comparing object references with , the
result is true if both references refer to the
same object in memory. - String method equals uses dictionary type
comparison of strings and returns true if they
have the exact same letters in the exact same
order.
16Evaluate each expression below
String s1 new String( today ) String
s2 today String s3 new String(
today ) String s4 new String ( Today
)
s1.equals(s2) s1 s3
s1.equals(s3) s1.equals(s4)
s1.substring(3, 5) s1.charAt(4)
s1.indexOf( (int) d ) s1.length( )
17Values of each expression
String s1 new String( today ) String
s2 today String s3 new String(
today ) String s4 new String ( Today
)
s1.equals(s2) true s1 s3 false
s1.equals(s3) true s1.equals(s4) false
s1.substring(3, 5) ay s1.charAt(4)
y s1.indexOf( (int) d ) 2
s1.length( ) 5
18Processing each character in a String
// Prints each character of String s on a
different line public void printEachLetter(
String s ) for ( int k 0 k lt
s.length( ) k ) System.out.println(
s.charAt( k ) )
19Count occurrences of a character in a String
// Counts number of times character ch appears
in String s public int countCharacter( String s,
char ch ) int count 0 for ( int k
0 k lt s.length( ) k ) if (
s.charAt( k ) ch ) count retu
rn count
20Java Strings are unalterable
- Once a Java String has been instantiated, it
cannot be changed in any way. - A StringBuffer should be used instead of a String
for a task that involves modifying a string. - A StringBuffer object can be modified, and can
grow and shrink in length as needed by using
StringBuffer methods.
21Two Different Classes
String
StringBuffer
Objects are modifiable character strings once
created. Has methods to build and modify a
string in the buffer. length ( ) charAt ( int i
) insert ( Type any, int i ) append ( Type any )
Objects are constant character strings, not
modifiable once created. length ( ) charAt (
int i )
22Capacity of a StringBuffer
- The capacity of a StringBuffer can expand
automatically when needed. - A StringBuffer is used to implement the
concatenation operator for String class.
g.drawString(value x ) // is
implemented as g.drawString( new
StringBuffer().append(value
).append(x).toString( ) )
23class StringBuffer constructors
// Constructors public StringBuffer( ) //
Constructs a new empty StringBuffer public
StringBuffer( String s ) // Constructs a new
StringBuffer with characters from s public
StringBuffer( char charArray ) // Constructs
a new StringBuffer from the character array
24class StringBuffer methods
// Instance Methods public StringBuffer
append( Type any ) // Adds value of 10 data
types at end of a StringBuffer public
StringBuffer insert( int index, Type any ) //
Inserts value of 9 data types in a StringBuffer
at index public String toString( ) // Converts
a StringBuffer to a String
25Java wrapper class Character
- Java Character class is part of java.lang
package. - There are many Character class static methods.
26What is the value of each expression?
char b char c b 2 c A
Character.isDigit( b ) Character.isDigit(
c ) Character.isUpperCase( c )
Character.toLowerCase( c )
Character.isLetter( c ) Character.isLetter(
b ) Character.toLowerCase( b )
Character.isLetterOrDigit( b )
27Value of each expression
char b char c b 2 c A
Character.isDigit( b ) true
Character.isDigit( c ) false
Character.isUpperCase( c ) true
Character.toLowerCase( c ) a
Character.isLetter( c ) true
Character.isLetter( b ) false
Character.toLowerCase( b ) 2
Character.isLetterOrDigit( b ) true
28StringTokenizer class
- Javas StringTokenizer class in package java.util
breaks a string into its component tokens, as
determined by a set of delimiters. - The delimiters can be the 4 default delimiters
(space, newline, tab, carriage return), or other
delimiters can be specified for the
StringTokenizer.
29StringTokenizer constructors
// 3 Constructors public StringTokenizer(
String s ) // Creates a StringTokenizer for
String s that will use // the default delimiter
string \n\t\r for tokenization public
StringTokenizer( String s, String delims ) //
Creates a StringTokenizer for String s that will
use // the characters in String delims for
tokenization public StringTokenizer( String s,
String delims, boolean b ) // Creates a
StringTokenizer for String s that will use //
the characters in String delims for tokenization,
and // returns the delimiters as tokens only if
b is true
30StringTokenizer methods
// StringTokenizer Instance Methods public
int countTokens( ) // Returns number of
individual tokens in the String public boolean
hasMoreTokens( ) // Returns true if there are
more tokens in the String, // and returns false
if there are no more tokens left public String
nextToken( ) // Returns a String with the next
token in the String // being tokenized
31Applet using StringTokenizer class
// Tests StringTokenizer class TokenTest.java
import java.applet.Applet import
java.util. // for StringTokenizer class
import java.awt. import
java.awt.event. public class TokenTest
extends Applet implements ActionListener
// GUI instance variables Label
prompt TextField input TextArea output
32Using StringTokenizer (contd)
// continued TokenTest.java //
initialize GUI instance variables public void
init() prompt new Label( Enter
sentence and press Enter ) input new
TextField( 50 ) input.addActionListener(
this ) output new TextArea( 10, 30 )
output.setEditable( false ) add( prompt
) add( input ) add( output )
33Using StringTokenizer (end)
// end TokenTest.java public void
actionPerformed( ActionEvent e ) String
stringToTokenize e.getActionCommand( )
StringTokenizer tokens new StringTokenizer(
stringToTokenize ) output.setText(
) output.append( Number of elements
tokens.countTokens( ) \nThe tokens
are\n ) while ( tokens.hasMoreTokens( )
) output.append( tokens.nextToken( ) \n )
34ACKNOWLEDGMENT
- This project was supported in part by a grant
from the National Science Foundation under grant
No. DUE-ATE 9950056.