SubString

Parameters
sourcestring

Text string.

startinteger

Starting byte position, zero-indexed, inclusive.

endinteger

Ending byte position, zero-indexed, exclusive.

comment

Returns a new substring from the interval [start, end) (inclusive, exclusive). Positions are zero-indexed. For empty or invalid out-of-bounds values returns an empty string "" (in Lua).

For start>end returns substring beginning with start until the actual end of string. For start<0 returns an empty string.

note

Reforged 2.0.4: start and end are UTF-8 byte offsets, matching StringLength. A slice can split a multi-byte character. The partial bytes are retained without an error or adjustment to a character boundary, and adjacent slices concatenate back to the original string exactly:

SubString("дä", 0, 1) .. SubString("дä", 1, 4) == "дä"
SubString("дä", 0, 2) == "д"

See StringHash for detecting a slice that starts with a multi-byte lead byte but ends before the character is complete.

Examples (Lua):

SubString("abc", 0, 0) == ""
SubString("abc", 0, 1) == "a"
SubString("abc", 2, 3) == "c"
SubString("abc", 0, 3) == "abc"
SubString("abcdef", 2, 0) == "cdef"
pure
This function is pure. For the same values passed to it, it will always return the same value.
patch

1.00

Source
common.j (raise an issue to discuss on Github or submit a PR)
return type
string
Source code
native SubString takes string source, integer start, integer end returns string