Completion requirements
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.
19. Tricky Rules
Answer:
art
applecart
Tricky Rules
Tricky Rules
The method
public String substring(int beginIndex )
creates a new string. The original string is not changed. There are two tricky rules:
- If
beginIndex
is exactly equal to the length of the original string, a substring is created, but it contains no characters (it is an empty string) - If
beginIndex
is greater than the length of the original string, or a negative value, anIndexOutOfBoundsException
is thrown (and for now, your program crashes).
Question 19:
What string is formed by the following expressions? (click on the button when you have decided)
Expression New String Comment String snake = "Rattlesnake"; snake.substring(6) snake.substring(0) snake.substring(10) snake.substring(11) snake.substring(12)