Chapter 10 - Strings. In this chapter, the learning objectives are: Learn about literal strings, learn about string constructors and commonly used methods, understand immutability of strings, learn to format numbers into strings and extract numbers from strings, learn several useful methods of the Character class, learn about the StringBuffer class.
Trang 1and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
Trang 2Objectives:
commonly used methods
extract numbers from strings
Character class
Trang 3The String class
string of characters.
• The String class belongs to the java.lang
package, which is built into Java.
and methods.
Trang 4Literal Strings
text in double quotes.
they are “just there.”
Trang 5Literal Strings (cont’d)
as parameters.
String fileName = "fish.dat";
button = new JButton("Next slide");
if ("Start" equals(cmd))
Trang 6Literal Strings (cont’d)
characters (described in Section 6.5) For example:
Trang 7Immutability
none of its methods can change the string.
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.
Trang 9Immutability (cont’d)
create a new string and throw away the old one for every small change.
Trang 10String s2 = new String();
private String errorMsg; errorMsg
is null
Empty strings
Trang 11Constructors
• String ’s no-args and copy constructors are not used much.
(Chapter 12) into strings
String s1 = new String ();
String s2 = new String (s1);
String s1 = ""; String s2 = s1;
Trang 12”Wind".charAt (2);
Returns:
Character positions in strings
are numbered starting from 0
Trang 13String s2 = s.substring (i, j);
returns the substring of chars in
positions from i to j-1
String s2 = s.substring (i);
returns the substring from the i-th
char to the end
Trang 15String date ="July 5, 2012 1:28:19 PM";
Trang 16returns true if the string s1 matches s2, case-blind
int diff = s1.compareTo(s2);
returns the “difference” s1 - s2
int diff = s1.compareToIgnoreCase(s2);
returns the “difference” s1 - s2, case-blind
Trang 17Methods — 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
Trang 19Integer and Double
are “wrapper” classes
from java.lang that
represent numbers as objects They also provide useful static methods.
Trang 20Numbers to Strings (cont’d)
• The DecimalFormat class can be used for formatting numbers into strings.
Trang 21Numbers to Strings (cont’d)
int m = 5, d = 19, y = 2007;
double amt = 123.5;
System.out.printf (
"Date: %02d/%02d/%d Amount = %7.2f\n", m, d, y, amt);
String s = String format(
"Date: %02d/%02d/%d Amount = %7.2f\n", m, d, y, amt);
Displays,
sets s to:
"Date: 05/19/2007 Amount 123.50"
Trang 22Numbers from Strings
NumberFormatException if s does not
represent a valid number (integer, real
number, respectively).
String s1 = "-123", s2 = "123.45";
int n = Integer.parseInt(s1);
double x = Double.parseDouble(s2);
Trang 24• Character also has methods that convert a letter to the upper or lower case.
Trang 26int d = Character.digit (ch, radix);
returns the int value of the digit ch in the given
int radix
char ch = Character.forDigit (d, radix);
returns a char that represents int d in a given
int radix
Trang 27The StringBuffer Class
mutable object
StringBuffer() // empty StringBuffer of the default capacity StringBuffer(n) // empty StringBuffer of a given capacity StringBuffer(str) // converts str into a StringBuffer
methods
• The toString method converts this
StringBuffer into a String
Trang 28Review:
character into a literal string?
• Is "length".length() allowed syntax? If so,
what is the returned value?
efficient or less efficient?
Trang 29Review (cont’d):
Trang 30Review (cont’d):
few overloaded versions.
compareTo methods?
String object?
Trang 31Review (cont’d):
a string.
String into an int ?
identify the category to which a given
character belongs.
StringBuffer classes?