Ocaml String.sub: How to get all characters until end of string

Viewed 369

So I'm using the String.sub function and I'm wondering if there's any way to get all the characters in a string until the end of the string. Since String.sub takes in a string, int (the index of where to start getting chars), and then a number of how many chars, I'm not sure what the easiest way of doing all chars since we want a possibly positive infinite amount

1 Answers

For String.sub you just have to subtract from the length of the string. There's no simpler way, i.e., there's no value for length that has a special meaning. A value larger than the remainder of the string is an error in OCaml (which tends to be strict when checking parameters).

Assume i is >= 0 and < length of the string:

String.sub s i (String.length s - i)

You can use Str.last_chars, but you still need to know how many characters you want. I.e., you still have to subtract from the length of the string.

Str.last_chars s (String.length s - i)
Related