Available inall subroutines.
Returns a substring of the byte string
s, starting from the byte
length. The substring is a copy of the original bytes.
length parameter is optional. If it's not specified, it means until the
end of the string.
offset parameter is zero-based. For example,
substr("abcdefg", 0, 3) is
If the requested range is partially outside the string
s, the returned string
is truncated. For example,
substr("abcdefg", 5, 3) is
If the requested range is completely outside the string
s, an unset value is
returned. For example,
substr("abc", 4, 2) returns an unset value, the edge
substr("abc", 3, 2) being
offset counts backwards from the end of the string
substr("abcdefg", -3, 2) is
length counts backwards from the end of the string
s with the
offset taken into account. For example,
substr("abcdefg", 1, -3) is
substr("abcdefg", -4, -3) is
An unset value is also returned in the extreme edge cases of the
length causing integer overflows.
substr() does not correctly handle UTF-8 encoded Unicode strings because byte offsets and lengths are likely to result in invalid UTF-8. Use
utf8.substr to handle UTF-8 encoded Unicode strings.
log "left=" substr("foobar", 0, 3)log "middle=" substr("foobar", 2, 3)log "right=" substr("foobar", -3)
substr is used in the following code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.
Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.
Extract a substring from a string value
Isolate a portion of a string identified by a range of characters.
Check validity of inputs using a non-crypto hash
Block or identify syntactically invalid requests at the edge by using a hash function of your choice.