GETRANGE
The GETRANGE
command in DiceDB is used to get a substring of a string, provided the start and end indices.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the key containing the string. | String | Yes |
start | The starting index of the required substring. | Integer | Yes |
end | The ending index of the required substring. | Integer | Yes |
Return values
Condition | Return Value |
---|---|
if key contains a valid string | a substring based on the start and end indices |
if start is greater than end | an empty string is returned |
if the end exceeds the length of the string present at key | the entire string is returned |
if the start is greater than the length of the string | an empty string is returned. |
Behaviour
- If the specified key does not exist, the
GETRANGE
command returns an empty string. - If
start
is greater thanend
, theGETRANGE
command returns an empty string. - If
start
is not within the length of the string, theGETRANGE
command returns an empty string. start
andend
can be negative which removedend + 1
characters from the other side of the string.- Both
start
andend
can be negative, which removes characters from the string, starting from theend + 1
position on the right.
Errors
-
Wrong type of value or key
:- Error Message:
(error) ERROR WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs when attempting to use the command on a key that contains a non-string value.
- Error Message:
-
Invalid syntax or conflicting options
:- Error Message:
(error) ERROR wrong number of arguments for 'GETRANGE' command
- If the number of arguments are not exactly equal to 4.
- Error Message:
-
Invalid input type for start and end
- Error Message:
(error) ERROR value is not an integer or out of range
- If
start
andend
are not integers.
- Error Message:
Example Usage
Basic Usage
Assume we have a string stored in foo
GETRANGE
returns string representation of byte array stored in bitmap
Invalid usage
Trying to use GETRANGE
without giving the value
Trying to use GETRANGE
on an invalid data type.