String dryden = " None but the brave deserves the fair. " ; System.out.println( dryden.trim() );
None but the brave deserves the fair.
(There are no printed spaces on either side of the above. The internal spaces remain.)
String
Objects
The characters that a String
object contains can
include control characters.
For example,
examine the following code:
class BeautyShock { public static void main (String[] arg) { String line1 = "Only to the wanderer comes\n"; String line2 = "Ever new this shock of beauty\n"; String poem = line1 + line2; System.out.print( poem ); } }
The sequence \n
represents the control characters
for a new line.
These control characters are part of the data contained in
the object referenced by poem
.
The program writes to the monitor:
Only to the wanderer comes Ever new this shock of beauty
Although you do not see them printed,
the control characters are part of the data in the String
.
Here is another method from the String
class:
public String toLowerCase();
This method
constructs a new String
object containing all lower case letters.
Here are some lines of code. Which ones are correct?
Line of code: | OK | Not OK |
---|---|---|
String line = "The Sky was like a WaterDrop" ; | ||
String a = line.toLowerCase(); | ||
String b = toLowerCase( line ); | ||
String c = toLowerCase( "IN THE SHADOW OF A THORN"); | ||
String d = "Clear, Tranquil, Beautiful".toLowerCase(); | ||
System.out.println( "Dark, forlorn...".toLowerCase() ); |