---
title: substr
summary: null
url: https://www.fastly.com/documentation/reference/vcl/functions/strings/substr
---

```
STRING substr(STRING s, INTEGER offset, INTEGER length?)
```

**Available in:** all subroutines

Returns a substring of the byte string `s`, starting from the byte `offset`, of
byte `length`. The substring is a copy of the original bytes.

The `length` parameter is optional. If it's not specified, it means until the
end of the string.

The `offset` parameter is zero-based. For example, `substr("abcdefg", 0, 3)` is
`"abc"`.

If the requested range is partially outside the string `s`, the returned string
is truncated. For example, `substr("abcdefg", 5, 3)` is `"fg"`.

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
case `substr("abc", 3, 2)` being `""`.

A negative `offset` counts backwards from the end of the string `s`. For
example, `substr("abcdefg", -3, 2)` is `"ef"`.

A negative `length` counts backwards from the end of the string `s` with the
`offset` taken into account. For example, `substr("abcdefg", 1, -3)` is `"bcd"`
and `substr("abcdefg", -4, -3)` is `"de"`.

An _unset_ value is also returned in the extreme edge cases of the `offset` or
`length` causing integer overflows.

> **IMPORTANT:** `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.

## Examples

```vcl
log "left=" substr("foobar", 0, 3)
log "middle=" substr("foobar", 2, 3)
log "right=" substr("foobar", -3)
```
