Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

374 Part II: The Java Library


String upper = s.toUpperCase();
String lower = s.toLowerCase();

System.out.println("Uppercase: " + upper);
System.out.println("Lowercase: " + lower);
}
}

The output produced by the program is shown here:

Original: This is a test.
Uppercase: THIS IS A TEST.
Lowercase: this is a test.

Additional String Methods


In addition to those methods discussed earlier,Stringincludes several other methods. These
are summarized in the following table. Notice that many were added by J2SE 5.

Method Description
int codePointAt(inti) Returns the Unicode code point at the location specified byi.
Added by J2SE 5.
int codePointBefore(inti) Returns the Unicode code point at the location that precedes
that specified byi.Added by J2SE 5.
int codePointCount(intstar t, intend) Returns the number of code points in the por tion of the invoking
Stringthat are betweenstar tandend–1. Added by J2SE 5.
boolean contains(CharSequencestr) Returnstrueif the invoking object contains the string specified
bystr. Returnsfalse, other wise. Added by J2SE 5.
boolean contentEquals(CharSequencestr) Returnstrueif the invoking string contains the same string as
str.Other wise, returnsfalse. Added by J2SE 5.
boolean contentEquals(StringBufferstr) Returnstrueif the invoking string contains the same string as
str.Other wise, returnsfalse.
static String format(Stringfmtstr,
Object ...args)

Returns a string formatted as specified byfmtstr.(See Chapter 18
for details on formatting.) Added by J2SE 5.
static String format(Localeloc,
Stringfmtstr,
Object ...args)

Returns a string formatted as specified byfmtstr.Formatting
is governed by the locale specified byloc.(See Chapter 18 for
details on formatting.) Added by J2SE 5.
boolean matches(stringregExp) Returnstrueif the invoking string matches the regular expression
passed inregExp.Other wise, returnsfalse.
int offsetByCodePoints(intstar t, intnum) Returns the index with the invoking string that isnumcode points
beyond the star ting index specified bystar t.Added by J2SE 5.
String
replaceFirst(StringregExp,
StringnewStr)

Returns a string in which the first substring that matches the
regular expression specified byregExpis replaced bynewStr.

String
replaceAll(StringregExp,
StringnewStr)

Returns a string in which all substrings that match the regular
expression specified byregExpare replaced bynewStr.
Free download pdf