Find the Index, Then Cut 🌶️🌶️
Nearly every String task on the AP exam has the same shape: find where something is, work out where the piece you want starts or stops, then cut it out with substring. This worksheet slows that down into its three steps so you can practice each one before you put them together in the coding problems.
| Step | Tool | What you get |
|---|---|---|
| 1. Find | text.indexOf(target) | the index where target starts, or -1 if it is not there |
| 2. Work out the boundary | + target.length(), + 1, and so on | the index where the piece you want starts (or stops) |
| 3. Cut | text.substring(from) or text.substring(from, to) | the piece itself |
A picture helps. Here are the indexes of the String
"Name: Ada":| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| character | N | a | m | e | : | (space) | A | d | a |
So
"Name: Ada".indexOf(": ") is 4, because the two-character target ": " starts at index 4. But the name we want does not start there. Work through the steps below.
1. Step 1: find. What does "key=value".indexOf("=") return? Write just the number.
3
2. What does "Name: Ada".indexOf(": ") return? (Use the table above.) Write just the number.
4
3. What does "abcabc".indexOf("c") return when the target appears twice? Write just the number.
2
4. What does "hello".indexOf("z") return, since there is no "z" in it? Write just the number.
-1
indexOf tells you where the target starts. If you want the piece that comes AFTER the target, you have to skip the whole target, which is target.length() characters long.5. Step 2: work out the boundary. In "Name: Ada", indexOf(": ") is 4. At which index does "Ada" start?
6. In "x==y", indexOf("==") is 1. At which index does the piece AFTER the "==" start? Write just the number.
3
substring(from) takes everything from from to the end. substring(from, to) takes from from up to but not including to. Now put the steps together.7. Step 3: cut. Let text be "key=value". What does text.substring(0, text.indexOf("=")) return? Write just the resulting characters, with no quotation marks.
key
8. Let text be "Name: Ada". What does text.substring(text.indexOf(": ") + 2) return? Write just the resulting characters, with no quotation marks.
Ada
9. Let text be "f(x)". What does text.substring(text.indexOf("(") + 1, text.indexOf(")")) return? Write just the resulting characters, with no quotation marks.
x
10. Build one statement that stores everything AFTER the colon and space in the String text into a variable named after. (Some tokens are distractors and won't be used.)
Click a placed token to send it back. With drag-and-drop enabled, you can also drag tokens to reorder them.
11. Build one statement that stores everything BEFORE the equals sign in the String text into a variable named before. (Some tokens are distractors and won't be used.)
Click a placed token to send it back. With drag-and-drop enabled, you can also drag tokens to reorder them.
12. Put these lines in order so that rest ends up holding everything after the target in text. (Two lines don't belong.)
Click a placed item to send it back. With drag-and-drop enabled, you can also drag items to reorder them — indent arrows always stay click-only.
13. A String text does NOT contain the letter z. What happens when this runs? String part = text.substring(text.indexOf("z"));
14. A second delimiter. Let date be "3/14/2024" and String rest = date.substring(2); so that rest is "14/2024". What does rest.indexOf("/") return? Write just the number.
2
15. That second slash is at index 2 of rest. At which index is it in the ORIGINAL "3/14/2024"? (Remember where rest began.) Write just the number.
4
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.