Q&A
How to get a substring in Java
Overview
substring() is one of the most-used String methods. The single concept to internalize is the half-open range: [beginIndex, endIndex) - the start is included, the end is excluded. Getting that boundary right (and handling edge cases) is what the question really tests.
graph LR
A["String: 'Educative'"] --> B["indices: 0..8"]
B --> C["substring(0,3) → 'Edu' (0,1,2; 3 excluded)"]
B --> D["substring(3) → 'cative' (index 3 to end)"]
style C fill:#1e1b4b,color:#fff
The Two Forms
String s = "Educative"; // indices 0..8, length 9
s.substring(3); // "cative" - from index 3 to the end
s.substring(0, 3); // "Edu" - indices 0,1,2 (3 is excluded)
s.substring(2, 5); // "uca" - indices 2,3,4
s.substring(0, s.length()); // "Educative" - whole string
s.substring(9); // "" - start == length → empty string (valid)
substring(beginIndex)- frombeginIndex(inclusive) to the end.substring(beginIndex, endIndex)- frombeginIndex(inclusive) toendIndex(exclusive).
The length of the result is endIndex - beginIndex.
Zero-Based, End-Exclusive
This is the crux. For "Hello":
H e l l o
0 1 2 3 4
"Hello".substring(1, 4) // "ell" - indices 1,2,3; index 4 ('o') excluded
The exclusive end means substring(i, j) returns j - i characters - convenient for length math.
Edge Cases and Exceptions
"Hello".substring(-1); // StringIndexOutOfBoundsException (begin < 0)
"Hello".substring(0, 99); // StringIndexOutOfBoundsException (end > length)
"Hello".substring(3, 1); // StringIndexOutOfBoundsException (begin > end)
"Hello".substring(5); // "" - begin == length is allowed (empty result)
"Hello".substring(2, 2); // "" - begin == end → empty string
Rules: 0 <= beginIndex <= endIndex <= length. Violating any of these throws StringIndexOutOfBoundsException.
Practical Patterns
String email = "user@example.com";
int at = email.indexOf('@');
String user = email.substring(0, at); // "user"
String domain = email.substring(at + 1); // "example.com"
String filename = "report.pdf";
String ext = filename.substring(filename.lastIndexOf('.') + 1); // "pdf"
Combining substring with indexOf/lastIndexOf is the standard way to split around a delimiter.
Immutability Note
Strings in Java are immutable - substring() returns a new String and never modifies the original:
String original = "Hello";
String part = original.substring(0, 3);
// original is still "Hello"; part is "Hel"
(Historically, older Java versions shared the backing char array; since Java 7u6 substring copies, avoiding memory-leak surprises.)
Why This Matters in Interviews
This checks whether you truly understand string indexing. The high-value points: zero-based indices, the end-exclusive range (so result length is end - begin), the valid-range rules and the exception thrown when violated, that beginIndex == length is legal (empty string), and that strings are immutable (a new string is returned). Pairing substring with indexOf/lastIndexOf for parsing shows fluency.
Common Pitfalls
- Treating
endIndexas inclusive (off-by-one - it's exclusive). - Passing an end index of
length - 1and missing the last character (uselength). - Negative or too-large indices →
StringIndexOutOfBoundsException. - Assuming
substringmutates the original (it returns a new string). beginIndex > endIndex(e.g. swapped arguments) throws.
References
- Java
String.substring: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html - Java String tutorial: https://docs.oracle.com/javase/tutorial/java/data/strings.html