Skip to main content

Description

Overwrites part of the string stored at key, starting at the specified offset, for the entire length of value. If the offset is larger than the current length of the string at key, the string is padded with zero-bytes to make offset fit. Non-existing keys are considered as empty strings.

Syntax

Parameters

key
string
required
The key containing the string to modify
offset
number
required
The position at which to start overwriting (0-based)
value
string
required
The string to write at the specified offset

Returns

length
number
The length of the string after it was modified by the command

Examples

Basic Usage

Overwrite in the Middle

Padding with Zero-Bytes

Setting on Non-Existent Key

Updating Specific Fields

Modifying Structured Data

Notes

  • The offset is zero-based
  • If the key does not exist, it is created as an empty string before performing the operation
  • If the offset is beyond the current string length, the string is padded with null bytes (\x00)
  • Time complexity is O(1), not counting the time taken to copy the new string in place
  • Returns the length of the string after modification
  • Be careful when using large offsets on non-existent keys, as this will create a large string filled with zero-bytes
  • getrange - Get a substring of the string stored at a key
  • append - Append a value to a key
  • set - Set the string value of a key

Redis Documentation

For more information, see the Redis SETRANGE documentation.