• Post Reply Bookmark Topic Watch Topic
  • New Topic

substring -1  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why does the substring method subtract an extra 1?

For example:

myString = "Programming and Problem Solving";

myString.substring(0, 7) would be "Program" instead of "Programm".

It's not explained in the book I'm reading and didn't see it online.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:
Why does the substring method subtract an extra 1?

For example:

myString = "Programming and Problem Solving";

myString.substring(0, 7) would be "Program" instead of "Programm".



Short answer... because that is how it is defined. The start index is inclusive. And the end index is exclusive.

Slightly longer answer as to why it is defined that way. The javadoc gives a hint. It is easy to calculate the length of the resultant string by simply subtracting the start index from the end index.

Henry
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason. If both begin and end indexes were inclusive, it would be impossible to extract an empty substring.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Another reason. If both begin and end indexes were inclusive, it would be impossible to extract an empty substring.


Whelp....glad you guys didn't give me the long version because I have no idea what this sentence even means
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,

Perhaps an example would help:

How does the computer count the index values of "Foo"?
F = 0
o = 1
o = 2

If the endIndex was inclusive, and I wanted a substring that was the first two letters of "Foo", I would need to write substring(0, 1). That would be odd, because length() would tell me the total length is 3 and to the human mind 2 makes more sense. Therefore, Java takes care of making sure "2" is adjusted to match the "1" value that the computer expects to be the endIndex by making that second value exclusive. If you are curious why the index starts at "0" instead of "1", that's because behind the scenes the String is held in a char array, and arrays always have a beginning index of 0.

Now, what Pawel was referring to is that substring(0,0) is completely valid (though, perhaps odd to do):

That will generate an empty String with a length of 0. If the endIndex was inclusive, I would have to write something like substring(0,-1), which obviously will throw an StringIndexOutOfBoundsException. It would be impossible to generate an empty substring, as substring(0, 0) would generate "F".

There is also a third possibility that's not so technical. The Java developers may have done it just to help C programmers transition to Java back in the day. C's strings have a hidden null terminating character that needs to be taken into account when performing many string functions in C.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris R Barrett wrote:Now, what Pawel was referring to is that subString(0,0) is completely valid (though, perhaps odd to do):

I agree. It would be odd to do it by explicitly.
However, sometimes one or both parameters of substring are calculated at runtime.
In this case it is perfectly valid to expect an empty substring.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you looked at the description of the substring method? Which bit did you find difficult? Which book have you got? Saying up to endIndex − 1 means that the last letter returned is one before the second index. That is equivalent to saying you don't get the letter at the second index only one before it, so the second index is “exclusive”.

I always think that you subtract the two numbers in substring.
0, 7 → 7 − 0 = 7 so you get a seven‑letter output. Actually for “letter” read “character” throughout.

That was a good answer Chris. Of course anybody with CR in their initials has got a good start in life
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That was a good answer Chris.

Thank you so much for the feedback. I feel a lot of pressure to live up to the other CR here. You have mighty big shoes...
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gee shucks, you have got me all embarrassed......
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for your help. I think understanding inclusive and exclusive is something I need to work on. Didn't hear of it until you guys wrote it.

As far as the book, I've ordered at least 10 but right now I'm reading Programming and Problem Solving. The other book I've mainly been reading is Java Programming by Joyce Farrell. I've found that book to be a hard read and confusing though. I've found Nell Dale's writing to be really helpful. Some of the criticisms I've read online are that it's too exhaustive on minor things but I find that helpful for me, especially with syntax.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
Always read the documentation. Find the all classes page and bookmark it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!