Skip to main content

Description

Returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive). Negative offsets can be used to provide an offset starting from the end of the string.

Syntax

Parameters

key
string
required
The key containing the string value
start
number
required
The starting offset (0-based). Negative values count from the end of the string
end
number
required
The ending offset (inclusive). Negative values count from the end of the string

Returns

substring
string
The substring determined by the start and end offsets

Examples

Basic Usage

Using Negative Offsets

Get Entire String

Extract Specific Parts

Non-Existent Key

Out of Range Offsets

Notes

  • Offsets are zero-based
  • Both start and end offsets are inclusive
  • Negative offsets count from the end: -1 is the last character, -2 is the second to last, etc.
  • If start is larger than the end of the string, an empty string is returned
  • If end is larger than the end of the string, the end of the string is used
  • Time complexity is O(N) where N is the length of the returned string
  • get - Get the value of a key
  • setrange - Overwrite part of a string at a specific offset

Redis Documentation

For more information, see the Redis GETRANGE documentation.