Reference Manual, Expressions

The description for usage of the substring(start, end) is slightly incorrect. Currently states:
substring(start,end) returns the substring from start to end , exclusive.
The exclusive applies only to the start. The end is really the length to truncate at.
For example - using abcdefgh as the string, the start as 0 and the end as 5 the expression will return abcde (5 characters). If the term exclusive was applied then the returned string would be abcd.
Just thought I’d bring this up.

1 Like

The description is accurate as-is. Whenever you have a range in computer science, it’s customary to indicate whether the right end is inclusive or exclusive. It’s extraordinarily unusual for the left end to be anything other than inclusive, so that’s omitted.

How many users ‘have a range in computer science’? I’m an electronics engineer. Been an engineer I like things to be specific, not implied. Besides that, your right and left ends are transposed. Last I looked abcde has the a on the left or start and the e is on the right or end. Call me anal retentive because that is what I am.

I still don’t see what’s wrong with the description. substring(start,end) is supposed to return the range [start,end), which in words is “from start to end, exclusive”.

Please give a specific example showing how the behavior doesn’t match the description.

see 1st post. I would word that particular phrase from start (exclusive) to end
I would probably also consider a note when using the substring functions - check the length before using substring to ensure the target string is long enough. Otherwise there will be an error.

Sorry, I sill don’t understand what you’re saying. Noting the start as exclusive would be incorrect. The start is supposed to be inclusive.

Is the first character in a string character 0?

Yes, everything, strings included, is indexed from zero.

So in example by @palad0n

The returned string “abcde” are characters 0 through 4 of “abcdefgh”. So the end, character 5, the “f”, is excluded.

Yes, the range [0,5) consists of the first five characters of the string. The character at position 5 is excluded, due to the range being open to the right (i.e., exclusive). This is as intended and matches the documentation.

Thank you. Some other research clarifies this further. Strings start at the first been 0. I believed that the first of a string would be 1. To me that is logical. Using 0 as the first character of a string is not logical, but that seems to be way this java stuff works. Go figure.
Remember that 0 was not even a number for ages.

It’s far from alone–I daresay nearly all of the heavily-used languages for software development are zero-indexed. Of those that aren’t, they’re either very antiquated or highly-specialized, e.g. for math/statistical packages.

1 Like

As an engineer that cut his programming “software” teeth on FORTRAN in the late 1970s, I’m more familiar with the idea of the index starting at 1 rather than 0. I guess FORTRAN is now considered antiquated.

Yes indeed. Fortran IV. Mainframe RMIT using punch cards. Made a small mistake with do loop, must have hit the 0 key many times. Loop went for 100,000 times. Huge, at the time, CPU usage.

1 Like

I wish the go to example would not start at 0. (Or 1.)

I usually need to check whether “substring” is given “start,end” or “start,length”. And whether “end” is really “first character not returned”.

So how about the string alphabet = “abcdefghijklmnopqrstuvwxyz”
So I’m thinking alphabet.substring(3,7) would be “defg”

1 Like

That would be correct.