name: duk_substring proto: | void duk_substring(duk_context *ctx, duk_idx_t idx, duk_size_t start_char_offset, duk_size_t end_char_offset); stack: | [ ... str! ... ] -> [ ... substr! ... ] summary: |

Replace a string at idx with a substring [start_char_offset, end_char_offset[ of the string itself. If the value at idx is not a string or the index is invalid, throws an error.

The substring operation works with characters, not bytes, and the offsets are character offsets. The semantics of this call are similar to String.prototype.substring (start, end). Offset values are clamped to string length (there is no need to clamp negative values because size_t is unsigned) and if start offset is larger than end offset, the result is an empty string.

To get a byte-oriented substring, use duk_get_lstring() to get a string data pointer and length, and duk_push_lstring() to push a slice of bytes.
example: | /* String at index -3 is 'foobar'. Substring [2,5[ is "oba". */ duk_substring(ctx, -3, 2, 5); printf("substring: %s\n", duk_get_string(ctx, -3)); tags: - string introduced: 1.0.0