Intervals
What are the output of the following snippets?
"hello world".substring(2, 5) // Java
std::string("hello world").substr(2, 5) // C++
Java gives "llo",
while C++ gives "llo w".
Even though they are both integers, they represent different things.
Here's "llo" from "hello world" written each way.
| Way | Written as | "llo" |
Seen in |
|---|---|---|---|
| Closed | [start, end] |
[2, 4] |
Haskell [1..10], Ruby 1..10, SQL BETWEEN |
| Half-open | [start, end) |
[2, 5) |
Java substring, Python xs[a:b], Ruby 1...10 |
| Start + length | (start, length) |
(2, 3) |
C memcpy, C++ substr, JS substr |