Java Methods

Download Report

Transcript Java Methods

Working with String
Duo Wei
CS110A_004
1
Empty Strings

An empty string has no characters; its
length is 0.
String s1 = "";
String s2 = new String();

Empty strings
Not to be confused with an uninitialized
string:
private String errorMsg;
errorMsg
is null
2
Constructors


String’s no-args and copy constructors are
not used much.
String s1 = new String ();
String s1 = "";
String s2 = new String (s1);
String s2 = s1;
Other constructors convert arrays into strings
(used in a lab in Chapter 10).
3
Methods — length, charAt
int length ();

Returns the number of
characters in the string
char charAt (k);

Returns the k-th char
Character positions in strings
are numbered starting from 0
Returns:
”Flower".length();
6
”Wind".charAt (2);
’n'
4
Methods — substring
String s2 = s.substring (i, k);
strawberry
i k
 returns
the substring of chars in
positions from i to k-1
String s2 = s.substring (i);
 returns
the substring from the i-th char to
the end
Returns:
”raw"
”strawberry".substring (2,5);
"happy"
"unhappy".substring (2);
"" (empty string)
"emptiness".substring (9);
5
Methods — Concatenation
String result = s1 + s2;
 concatenates
s1 and s2
String result = s1.concat (s2);
 the
same as s1 + s2
result += s3;
 concatenates
s3 to result
result += num;
 converts
num to String and
concatenates it to result
6
Methods — Find (indexOf)
0
8
11
15
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
date.indexOf ("2020");
-1
date.lastIndexOf ('2');
15
(starts searching
at position 9)
(not found)
7
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
int diff = s1.compareToIgnoreCase(s2);
 returns
the “difference” s1 - s2, case-blind
8
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
9
Replacements (cont’d)

Example: how to convert s1 to upper
s1 = s1.toUpperCase();
case

A common bug:
s1.toUpperCase();
s1 remains
unchanged
10
Methods — toString

It is customary to provide a toString
method for your class.

toString converts an object into a
String (for printing it out, for debugging,
System.out.print
(obj);
etc.).
System.out.print
is the same as(obj.toString());
11
Numbers to Strings and
Strings to Numbers

Integer and Double are “wrapper” classes
from java.lang that represent numbers as
objects.

Integer and Double provide useful static
methods for conversions:
String s1 = Integer.toString (i); int i;
String s2 = Double.toString (d); double d;
int n = Integer.parseInt (s1);
double x = Double.parseDouble (s2);
12
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);
int i;
double d;
3.
String s = String.valueOf (num);
13