Consider the following code segment. What is printed as a result of executing the code segment?
String s = "planet";
String part = s.substring(2, 5);
System.out.println(part);
plaaneanetnetsubstring(2, 5) starts at index 2 and stops before index 5, so it returns ane.
Explanation
A two-argument substring call includes the character at the start index and excludes the character at the end index. In "planet" the characters at indices 2, 3, and 4 are a, n, and e.
- A is not correct:
plais the first three characters, which is what a start index of0would produce. - C is not correct:
anetincludes the character at index5, but the end index is excluded. - D is not correct:
netbegins at index3, one position past the start index given in the call.