No. Negative indices are not allowed.

From String#substring(int beginIndex, int endIndex):

Throws: IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

While the documentation does not directly state that endIndex cannot be negative1, this can be derived. Rewriting the relevant requirements yields these facts:

  1. "beginIndex cannot be negative" -> beginIndex >= 0
  2. "beginIndex must be smaller than or equal to endIndex" -> endIndex >= beginIndex

Thus it is a requirement that endIndex >= beginIndex >= 0 which means that endIndex cannot be negative.


Anyway, str.substring(0, -x) can be trivially rewritten as str.substring(0, str.length() - x), assuming we have the same idea of what the negative end index should mean. The original bound requirements still apply of course.


1 Curiously, String#subSequence does explicitly forbid a negative endIndex. Given such, it feels that the documentation could be cleaned up such that both methods share the same simplified precondition text. (As a bonus: there is also an important typo in the "Java 7" subSequence documentation.)

Answer from user2864740 on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
February 20, 2025 - Both the string substring methods can throw IndexOutOfBoundsException if any of the below conditions met. ... Here is a simple program for the substring in java.
🌐
Java2s
java2s.com › ref › java › java-string-substring-from-negative-index.html
Java String substring from negative index
public class Main { public static void main(String[] argv) throws Exception { String str = "demo2s.com"; int start = -2; System.out.println(substring(str, start)); }//from w w w .j a v a 2 s. com public static final String EMPTY = ""; public static String substring(String str, int start) { if (str == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = str.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > str.length()) { return EMPTY; } return str.substring(start); } /** * <p>Gets a substring from the specified String avoiding exceptions.</p> * * <p>A negative start position can be used to start/end <code>n</code> * characters from the end of the String.</p> * * <p>The returned substring starts with the character in the <code>start</code> * position and ends before the <code>end</code> position.
🌐
GitHub
github.com › google › guava › issues › 1124
Strings.substring with negative indices · Issue #1124 · google/guava
October 31, 2014 - public static String substring(String s, int start, int end) { int len = s.length(); return s.substring(start >= 0 ? start : len + start, end >= 0 ?
Published   Oct 31, 2014
🌐
iNTERFACEWARE
help.interfaceware.com › kb › using-negative-indexes-with-stringsub
Using negative indexes with string:sub() - iNTERFACEWARE Help Center
Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length).
🌐
W3Resource
w3resource.com › java-tutorial › string › string_substring.php
Java String: substring Method - w3resource
String new_str = str.substring(10, 26); System.out.println(); // Display the two strings for comparison. System.out.println("old = " + str); System.out.println("new = " + new_str); System.out.println(); } } ... IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object. ... Exception in thread "main" java.lang.StringIndexOutOfBo undsException: String index out of range: -10 at java.lang.String.substring(String.java:1960) at Exercise.main(Exercise.java:9)
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › substring
Java String substring() - Extract Substring | Vultr Docs
January 1, 2025 - It is essential to handle potential exceptions that might arise from inappropriate index values: IndexOutOfBoundsException can occur if either beginIndex is negative, endIndex is larger ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - StringIndexOutOfBoundsException: This exception will happen when the index value is in cases such as negative beginIndex or an endIndex is greater than the length of the string or beginIndex is greater than the endIndex. Note: If we pass the start index and endIndex with the same value so it will return an empty substring (""). Example 1: Java program to extract a substring using substring(int beginIndex).
Find elsewhere
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java String Slicing Example - Java Code Geeks
March 20, 2025 - The method prints the extracted substring based on the provided indices. Negative Indexing: This Java method, negativeIndexing, allows extracting a substring using negative indices, similar to Python.
🌐
Java67
java67.com › 2016 › 01 › 5-examples-of-substring-in-java.html
5 Examples of substring() in Java | Java67
public String substring(int beginIndex) public String substring(int beginIndex, int endIndex) In the case of the first method, substring starts with beginIndex and goes till the end of String, while, in the case of an overloaded method, substring starts from beginIndex and goes till endIndex -1. Since String in Java is zero indexes based, beginIndex can be from 0 to length of String.
🌐
Baeldung
baeldung.com › home › java › java string › java equivalent to python’s easy string slicing
Java Equivalent to Python’s Easy String Slicing | Baeldung
March 25, 2025 - When only the start index is provided, Java’s substring(start) extracts from start to the end of the string, mimicking Python’s behavior. Python allows negative indices to count from the end:
🌐
CodingTechRoom
codingtechroom.com › question › java-negative-index-of
How to Use Negative Indexing in Java's String `indexOf` Method to Count from the End? - CodingTechRoom
Solution: Remember that Java does not support negative indexes; instead, calculate positions manually. Mistake: Using `indexOf` assuming it will return -1 for negative notations. Solution: Always check if the result of `indexOf` is -1 to handle cases where the substring is not found.
🌐
Rollbar
rollbar.com › home › how to handle string index out of bounds exception in java
How to Handle String Index Out Of Bounds Exception in Java
September 28, 2022 - The StringIndexOutOfBoundsException is thrown if any index is negative, the endIndex is greater than the length of the string or the beginIndex is greater than the endIndex. String.substring(int beginIndex) - Returns a substring beginning with ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java substring() method – tutorial with examples
Java substring() Method - Tutorial With Examples
April 1, 2025 - As the Java String index starts from zero, it should not accept negative integers in the index. So the program must throw an exception. The type of error should again be the “String index out of range” exception because the specified index ...
🌐
Vocal Media
vocal.media › education › common-mistakes-to-avoid-when-using-substring-in-java-h9d032j
Common Mistakes to Avoid When Using Substring in Java | Education
It's crucial to remember that when ... of the substring while the ending index points to the character immediately after the last character of the substring. ... In Java, when dealing with strings, it's imperative to consider negative indices to access substrings ...
🌐
Medium
medium.com › @rahul.tpointtech12 › a-closer-look-at-the-substring-method-in-java-78d722965433
A Closer Look at the Substring Method in Java | by Rahul | Medium
March 21, 2025 - When using substring, always ensure ... IndexOutOfBoundsException. Java will throw this exception if beginIndex is negative, endIndex is greater than the length of the string, or beginIndex is greater than endIndex....
🌐
W3Schools
w3schools.com › java › ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The substring() method returns a substring from the string.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › String.html
String (Java SE 11 & JDK 11 )
January 20, 2026 - If it is negative, it has the same effect as if it were -1: -1 is returned. ... the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.