Skip to main content
Misread documentation.
Source Link

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike manysome other methods, even within the same classin other classes. Some example snippets would be:

Example 1:

4: StringBuilder sb = new StringBuilder("abcdef");
5: sb.delete(2, 4);
6: System.out.println(sb);
//This would print abef instead of abf

Example 2:

4: String str = "abcdef";
5: String newStr = str.substring(2, 4);
6: System.out.println(newStr);
//This would print cd instead of cde

Does this seem strange to anyone else considering how mostsome other methods with index parameters work? Is there a reason for this? If so, please explain it to me.

Edit to differentiate and clarify unique question: This question is different from the discussion on solely substring() as it is about all methods that use indexcies to indicate that the method should stop before the provided endIndex (exclusion instead of inclusion). While many other methods, even in the same class, use endIndex inclusively.

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike many other methods, even within the same class. Some example snippets would be:

Example 1:

4: StringBuilder sb = new StringBuilder("abcdef");
5: sb.delete(2, 4);
6: System.out.println(sb);
//This would print abef instead of abf

Example 2:

4: String str = "abcdef";
5: String newStr = str.substring(2, 4);
6: System.out.println(newStr);
//This would print cd instead of cde

Does this seem strange to anyone else considering how most other methods with index parameters work? Is there a reason for this? If so, please explain it to me.

Edit to differentiate and clarify unique question: This question is different from the discussion on solely substring() as it is about all methods that use indexcies to indicate that the method should stop before the provided endIndex (exclusion instead of inclusion). While many other methods, even in the same class, use endIndex inclusively.

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike some other methods, in other classes. Some example snippets would be:

Example 1:

4: StringBuilder sb = new StringBuilder("abcdef");
5: sb.delete(2, 4);
6: System.out.println(sb);
//This would print abef instead of abf

Example 2:

4: String str = "abcdef";
5: String newStr = str.substring(2, 4);
6: System.out.println(newStr);
//This would print cd instead of cde

Does this seem strange to anyone else considering how some other methods with index parameters work? Is there a reason for this? If so, please explain it to me.

Edit to differentiate and clarify unique question: This question is different from the discussion on solely substring() as it is about all methods that use indexcies to indicate that the method should stop before the provided endIndex (exclusion instead of inclusion).

Clarified question in edit as it was marked as a possible repeat of a question specifically about substring().
Source Link

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike many other methods, even within the same class. Some example snippets would be:

Example 1:

4: StringBuilder sb = new StringBuilder("abcdef");
5: sb.delete(2, 4);
6: System.out.println(sb);
//This would print abef instead of abf

Example 2:

4: String str = "abcdef";
5: String newStr = str.substring(2, 4);
6: System.out.println(newStr);
//This would print cd instead of cde

Does this seem strange to anyone else considering how most other methods with index parameters work? Is there a reason for this? If so, please explain it to me.

Edit to differentiate and clarify unique question: This question is different from the discussion on solely substring() as it is about all methods that use indexcies to indicate that the method should stop before the provided endIndex (exclusion instead of inclusion). While many other methods, even in the same class, use endIndex inclusively.

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike many other methods, even within the same class. Some example snippets would be:

Example 1:

4: StringBuilder sb = new StringBuilder("abcdef");
5: sb.delete(2, 4);
6: System.out.println(sb);
//This would print abef instead of abf

Example 2:

4: String str = "abcdef";
5: String newStr = str.substring(2, 4);
6: System.out.println(newStr);
//This would print cd instead of cde

Does this seem strange to anyone else considering how most other methods with index parameters work? Is there a reason for this? If so, please explain it to me.

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike many other methods, even within the same class. Some example snippets would be:

Example 1:

4: StringBuilder sb = new StringBuilder("abcdef");
5: sb.delete(2, 4);
6: System.out.println(sb);
//This would print abef instead of abf

Example 2:

4: String str = "abcdef";
5: String newStr = str.substring(2, 4);
6: System.out.println(newStr);
//This would print cd instead of cde

Does this seem strange to anyone else considering how most other methods with index parameters work? Is there a reason for this? If so, please explain it to me.

Edit to differentiate and clarify unique question: This question is different from the discussion on solely substring() as it is about all methods that use indexcies to indicate that the method should stop before the provided endIndex (exclusion instead of inclusion). While many other methods, even in the same class, use endIndex inclusively.

Source Link

Java String substring() and StringBuilder delete() methods

I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike many other methods, even within the same class. Some example snippets would be:

Example 1:

4: StringBuilder sb = new StringBuilder("abcdef");
5: sb.delete(2, 4);
6: System.out.println(sb);
//This would print abef instead of abf

Example 2:

4: String str = "abcdef";
5: String newStr = str.substring(2, 4);
6: System.out.println(newStr);
//This would print cd instead of cde

Does this seem strange to anyone else considering how most other methods with index parameters work? Is there a reason for this? If so, please explain it to me.