substr(str, offset, length) returns the portion of str starting at offset with at most length characters.
Signature
offset is zero-based. A negative offset counts from the end of the string. Pass -1 for length to return everything from offset to the end of the string.
Example
Use the first 8 characters of a full commit SHA as a short identifier in Docker image tags:a3f9e21b4d6c8f12... becomes a3f9e21b.
Notes
offsetandlengthcount grapheme clusters, not bytes or code points.- If
offsetis beyond the end of the string, the result is an empty string. - If
lengthexceeds the available characters fromoffset, the result is truncated to the string end. - Use
length = -1to extract from a position to the end:substr(str, 2, -1).