The String class is used for text manipulation. As you read, you will learn different ways to create Strings, methods to manipulate Strings, the String concatenation operator '+', and about how Strings are immutable.
22. Control Characters inside String Objects
Answer:
Expression New String Comment String snake = "Rattlesnake"; "Rattlesnake" Create original String snake.substring(0,6) "Rattle" Characters starting at character 0 to character 6-1 snake.substring(0,11) "Rattlesnake" 0 to length includes all the characters of the original string snake.substring(10, 11) "e" Character 10 is the last one snake.substring(6,6) "" If beginIndex==endIndex, an empty substring is created. snake.substring(5,3) If beginIndex is greater than endIndex, an exception is thrown. snake.substring(5,12) if endIndex is greater than length, an exception is thrown.
Control Characters inside String
Objects
Control Characters inside String
Objects
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 ); } } |
Recall that control characters are bit patterns that indicate such things as the end of a line or page separations. Other control characters represent the mechanical activities of old communications equipment. The characters that a String
object contains can include control characters. Examine the program.
The sequence \n
represents the control character that ends a line. This control character is embedded in the data of the strings. The object referenced by poem
has two of them. The program writes to the monitor:
Only to the wanderer comes
Ever new this shock of beauty
public String toLowerCase();
This method constructs a new String
object containing all lower case letters. There is also a method that produces a new String
object containing all upper case letters:
public String toUpperCase();
Question 22:
Here are some lines of code. Which ones are correct?