Redis GETRANGE – How to get a substring of a string value in redis

2 months ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get a substring of the string value stored at a specified key in redis datastore. For this, we will use a Redis GETRANGE command.


This command takes start ( inclusive ) and end ( inclusive ) offsets, which determined the start and end index of a substring. The index is zero based, so 0 means the first element, 1 means the second element and so on. A negative number can also be used to provide an offset starting from the end of the string value, here -1 means the last element, -2 means second last element and so on. An out of range offsets are handled by limiting the resulting range to the real length of the string value. The syntax of Redis GETRANGE command is as follows :-

Syntax :-

redis host:post> GETRANGE <key name> <start> <end>

Output :- 

(string) value, representing the substring of a string value.

Example :-


References :-

  1. GETRANGE Command Docs

That’s all for how to get a substring of the string value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.

<- String Commands