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.
21. More Tricky Rules
Answer:
*
**
***
****
*****
More Tricky Rules
More Tricky Rules
As with the other substring method, this one
public String substring(int beginIndex, int endIndex )
creates a new string. The original string is not changed. Here are some more rules. Essentially, the rules say that if the indexes make no sense, a IndexOutOfBoundsException
is thrown.
- If
beginIndex
is negative value, anIndexOutOfBoundsException
is thrown. - If
beginIndex
is larger thanendIndex
, anIndexOutOfBoundsException
is thrown. - If
endIndex
is larger than the length, anIndexOutOfBoundsException
is thrown. - If
beginIndex
equalsendIndex
, and both are within range, then an empty string is returned.
Usually, when an exception is thrown your program halts. Future chapters discuss how to deal with exceptions
Question 21:
Decide on what string is formed by the following expressions: