substring: Bounds and Overloads 🌶️🌶️
substring is the method you will reach for constantly, and it is also the easiest one to get wrong — because it is overloaded (there are two versions) and because its two-argument version does something slightly surprising with its second argument.
| Call | What you get back |
|---|---|
s.substring(from, to) | the characters starting at index from, up to but NOT including index to |
s.substring(from) | the characters from index from all the way to the end — exactly the same as writing s.substring(from, s.length()) |
The second argument is the index to stop before, not the last index you get. Two consequences worth memorizing:
- The last character you actually receive is the one at index
to - 1. - The number of characters you get back is exactly
to - from. That subtraction is the fastest way to check yourself.
Because
to is exclusive, some calls that look like mistakes are perfectly legal and simply hand back an empty String — while others really are errors and throw a StringIndexOutOfBoundsException. Sorting out which is which is most of this worksheet.
1. What does "computer".substring(2, 5) return? Write just the resulting characters, with no quotation marks around them.
mpu
2. Without knowing what String it is called on, how many characters does s.substring(3, 9) return?
3. What does "chocolate".substring(3, 6) return? Write just the resulting characters, with no quotation marks around them.
col
4. Which call is exactly equivalent to s.substring(4)?
5. What does "strawberry".substring(5) return? Write just the resulting characters, with no quotation marks around them.
berry
6. You want just the single character at index 2 of a String s, as a one-character String. Which call gives you that?
7. What does "Java".substring(2, 3) return? Write just the resulting characters, with no quotation marks around them.
v
8. What does "hello".substring(5) do? (Note that "hello" has exactly 5 characters, so index 5 is one past the end.)
9. What does "hello".substring(3, 1) do?
10. What does "hello".substring(2, 2) return?
11. Arrange these tokens to build an expression that evaluates to the LAST character of a String s, as a one-character String. Use the one-argument version of substring. (Two tokens below are distractors.)
Click a placed token to send it back. With drag-and-drop enabled, you can also drag tokens to reorder them.
12. Trace the values of word and part immediately after each numbered line executes. Write String values as just their characters, with no quotation marks — for example key, not "key".
Pay close attention to what happens to word on lines 4 and 5, and then on line 6.
public void demo() { String word = "keyboard"; String part; part = word.substring(0, 3); part = part + word.substring(4, 6); word = word.substring(3);}
If a variable hasn't been declared, write DNE for Does Not Exist. If a variable has been declared but not initialized, write ?. Otherwise, fill in the value of each variable immediately after the labeled line finishes executing.
Your score for your class is recorded when you click Submit all — checking individual questions doesn't save it. You can submit again to improve your score; your best one counts.