Professional Documents
Culture Documents
A & AB
Object-Oriented Programming and Data Structures
Maria Litvin Gary Litvin
Copyright 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.
Objectives:
Learn about literal strings Learn about String constructors and
commonly used methods
10-3
Literal Strings
Literal strings are anonymous constant
objects of the String class that are defined as text in double quotes.
10-4
10-5
\\ stands for \ \n stands for the newline character String s1 = "Biology; String s2 = "C:\\jdk1.4\\docs"; String s3 = "Hello\n";
10-6
Immutability
Once created, a string cannot be changed:
none of its methods changes the string.
Such objects are called immutable. Immutable objects are convenient because
several references can point to the same object safely: there is no danger of changing an object through one reference without the others being aware of the change.
10-7
Immutability (contd)
Advantage: more efficient, no need to copy.
String s1 = "Sun"; String s2 = s1; String s1 = "Sun"; String s2 = new String(s1);
s1
s1
"Sun"
"Sun"
s2 OK s2
"Sun"
Immutability (contd)
Disadvantage: less efficient you need to
create a new string and throw away the old one for every small change.
String s = "sun"; char ch = Character.toUpperCase(s.charAt (0)); s = ch + s.substring (1); s
"sun"
"Sun"
10-9
Empty Strings
An empty string has no characters; its
length is 0.
String s1 = ""; String s2 = new String();
Empty strings
10-10
Constructors
Strings no-args and copy constructors are
not used much.
String s1 = new String (); String s1 = "";
String s2 = s1;
10-11
Flower".length();
Wind".charAt (2);
6 n'
10-12
Methods substring
String s2 = s.substring (i, k); returns the substring of chars in positions from i to k-1 strawberry i k
String s2 = s.substring (i); returns the substring from the i-th char to the end
strawberry".substring (2,5); "unhappy".substring (2); "emptiness".substring (9);
strawberry
i
Methods Concatenation
String result = s1 + s2;
concatenates s1 and s2
result += s3;
concatenates s3 to result
result += num;
converts num to String and concatenates it to result
10-14
String date ="July 5, 2012 1:28:19 PM"; date.indexOf ('J'); date.indexOf ('2'); date.indexOf ("2012"); date.indexOf ('2', 9); Returns: 0 8 8 11 (starts searching at position 9) (not found)
10-15
date.indexOf ("2020");
date.lastIndexOf ('2');
-1
15
Methods Comparisons
boolean b = s1.equals(s2); returns true if the string s1 is equal to s2 boolean b = s1.equalsIgnoreCase(s2); returns true if the string s1 matches s2, case-blind int diff = s1.compareTo(s2); returns the difference s1 - s2
10-16
Methods Replacements
String s2 = s1.trim (); returns a new string formed from s1 by removing white space at both ends String s2 = s1.replace(oldCh, newCh); returns a new string formed from s1 by replacing all occurrences of oldCh with newCh String s2 = s1.toUpperCase(); String s2 = s1.toLowerCase(); returns a new string formed from s1 by converting its characters to upper (lower) case
10-17
Replacements (contd)
Example: how to convert s1 to upper case
s1 = s1.toUpperCase();
A common bug:
s1.toUpperCase(); s1 remains unchanged
10-18
Numbers to Strings
Three ways to convert a number into a
string:
1. String s = "" + num; 2. String s = Integer.toString (i); String s = Double.toString (d); 3. String s = String.valueOf (num);
Integer and Double are wrapper classes from java.lang that represent numbers as objects. They also provide useful static methods.
10-19
56.7381 "56.74"
10-20
10-22
Character Methods
java.lang.Character is a wrapper class that
represents characters as objects.
10-24
.isUpperCase...
.isLowerCase... .isWhitespace...
10-25
10-26
Constructors:
StringBuffer() // empty StringBuffer of the default capacity StringBuffer(n) // empty StringBuffer of a given capacity StringBuffer(str) // converts str into a StringBuffer
Review:
What makes the String class unusual? How can you include a double quote
character into a literal string?
10-28
Review (contd):
How do you declare an empty string? Why are String constructors not used very
often?
Review (contd):
What do the indexOf methods do? Name a
few overloaded versions.
What does the trim method do? What does s.toUpperCase() do to s? What does the toString method return for a
String object?
10-30
Review (contd):
Name a simple way to convert a number into
a string.