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:
- "beginIndex cannot be negative" ->
beginIndex >= 0 - "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.)
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:
- "beginIndex cannot be negative" ->
beginIndex >= 0 - "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.)
No, using negative numbers for the endindex of substring as well as a number greater than the string length will result in an StringIndexOutOfBoundsException.
(You wouldn't believe how hard it was to find a straight answer to this on the net)
No, negative numbers are not allowed (it would throw a StringIndexOutOfBoundsException). You can use this:
if(month.length() >= 4) {
year = month.substring(month.length() - 4);
}
I love Python's indexing and prefer to be able to use it in Java as well ;)
A general implementation of substring with support for negative indexing could be implemented like this:
A method for converting negative indexes to their positive equivalent is useful:
private static int toPosIndex(String str, int index) {
if (index >= 0) return index;
else return str.length() + index;
This method can then be used to declare a substring method with support for negative indexes:
public static String negSubstring(String str, int index1, int index2) {
return str.substring(toPosIndex(str, index1), toPosIndex(str, index2));
}
and if needed an equivalent to the version of substring with only one index parameter:
public static String negSubstring(String str, int index) {
return str.substring(toPosIndex(str, index));
}
(I realise the question is more than two years old, but figured that other new users might be interested in a general answer to the question).
Add 26 then mod 26:
i = (i + 26) % 26;
This always works for indexes down to -26. If that's not enough, just add some zeroes:
i = (i + 26000000) % 26;
Your general problem appears to be that letters are represented by only 26 indices, but the actual index variable you use might be greater than 26, or even less than zero (negative). One way to handle this problem is to use the mod operator to safely wrap your index around to always point to a range containing a valid letter.
Here is logic which can do that:
if (index < 0) {
index = (index % 26) + 26;
}
else {
index = index % 26;
}
Assuming the letter E is position 5 and you have a reassignment of -7, this would mean that the new index would be -2. This new position can be mapped using the above logic as follows, where index = -2 in this case:
5 - 7 = -2
(-2 % 26) + 26
-2 + 26
24
And character in position 24 is the letter X.