Summary
Command Name | GETRANGE |
Usage | Get a substring by range |
Group | string |
ACL Category | @read @string @slow |
Time Complexity | O(N) **N is the length of the returned string |
Flag | READONLY |
Arity | 4 |
Notes
- The time complexity is based on the resulting substring. It does not depend on the size of the original string.
- For small strings, the complexity will not change that much. In that case, we can consider the time complexity as O(1).
Signature
GETRANGE <key> <start_index> <end_index>
Usage
Get substring from a string value. The substring is obtained from the specified <key> and the offsets are defined by the <start_index> and <end_index>.
Notes
- As the command “SUBSTR” is deprecated in Redis, so this “GETRANGE” command is a substitute for that command.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | Name of the key | key | key |
<start_index> | Starting index of the substring | start | integer |
<end_index> | Ending index of the substring | end | integer |
Notes
- Counting of the index starts from Zero(0).
- Indexes can be positive or negative. A negative index means counting the index in reverse order (-1 index is the last element)
Return Value
Return value | Case for the return value | Type |
---|---|---|
Subtring from the start to the ending index | If the key is valid and indexes are correct | string |
Blank string (“”) | In case of wrong/non-existing key. Or the provided indexes are wrong. | string |
error | If data is not of string type | error |
Notes
- Both the start and end index are inclusive(included in the result substring).
Examples
Here are a few examples of the GETRANGE command usage-
# Redis GETRANGE command examples
# Set some string value for description key
127.0.0.1:6379> set description "some long string for GETRANGE testing"
OK
# Get substring from description from index 0 to 10
127.0.0.1:6379> getrange description 0 10
"some long s"
# Get substring from description from index 0 to 1
127.0.0.1:6379> getrange description 0 1
"so"
# Get substring from description from index 0 to -1
127.0.0.1:6379> getrange description 0 -1
"some long string for GETRANGE testing"
# Get substring from description from index 20 to -1
127.0.0.1:6379> getrange description 20 -1
" GETRANGE testing"
# Get substring from description from index -5 to -1
127.0.0.1:6379> getrange description -5 -1
"sting"
# Get substring from description from index 20 to 10
# It will return empty string as the starting index is of a later element
127.0.0.1:6379> getrange description 20 10
""
# Get substring from description from index -1 to -5
# It will return empty string as the starting index is of a later element
127.0.0.1:6379> getrange description -1 -5
""
# Get substring from description from index 10 to 2000000
# As last index is out of range so the result will stop at the end of the source string
127.0.0.1:6379> getrange description 10 2000000
"string for GETRANGE testing"
# Get substring from description from index 5 to 5
127.0.0.1:6379> getrange description 5 5
"l"
# Try to get substring from a key that is not set.
# Returns an empty string.
127.0.0.1:6379> getrange wrongkey 10 20
""
# Create a list
127.0.0.1:6379> lpush mylist abcd
(integer) 1
# Try to get a substring by index, from the list
# returns error
127.0.0.1:6379> getrange mylist 0 2
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- If the provided index is out of range, then the result is restricted to the maximum length of the string.
- If the start index is larger(index of a later element), then the result will be an empty string. For example-
<start_index>: 20 and <end_index>: 10
or <start_index>: -1 and <end_index>:-5 - If the key is not defined then result will be empty string.
- If key data is not of type string then the following error message is returned-
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Code Implementations
Here are the usage examples of the Redis GETRANGE command in different programming languages.
// Redis GETRANGE command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
// Set some string value for description key
// Command: set description "some long string for GETRANGE testing"
// Result: OK
setResult, err := rdb.Set(ctx, "description", "some long string for GETRANGE testing", 0).Result()
if err != nil {
fmt.Println("Command: set description "some long string for GETRANGE testing" | Error: " + err.Error())
}
fmt.Println("Command: set description "some long string for GETRANGE testing" | Result: " + setResult)
// Get substring from description from index 0 to 10
// Command: getrange description 0 10
// Result: "some long s"
getRangeResult, err := rdb.GetRange(ctx, "description", 0, 10).Result()
if err != nil {
fmt.Println("Command: getrange description 0 10 | Error: " + err.Error())
}
fmt.Println("Command: getrange description 0 10 | Result: " + getRangeResult)
// Get substring from description from index 0 to 1
// Command: getrange description 0 1
// Result: "so"
getRangeResult, err = rdb.GetRange(ctx, "description", 0, 1).Result()
if err != nil {
fmt.Println("Command: getrange description 0 1 | Error: " + err.Error())
}
fmt.Println("Command: getrange description 0 1 | Result: " + getRangeResult)
// Get substring from description from index 0 to -1
// Command: getrange description 0 -1
// Result: "some long string for GETRANGE testing"
getRangeResult, err = rdb.GetRange(ctx, "description", 0, -1).Result()
if err != nil {
fmt.Println("Command: getrange description 0 -1 | Error: " + err.Error())
}
fmt.Println("Command: getrange description 0 -1 | Result: " + getRangeResult)
// Get substring from description from index 20 to -1
// Command: getrange description 20 -1
// Result: " GETRANGE testing"
getRangeResult, err = rdb.GetRange(ctx, "description", 20, -1).Result()
if err != nil {
fmt.Println("Command: getrange description 20 -1 | Error: " + err.Error())
}
fmt.Println("Command: getrange description 20 -1 | Result: " + getRangeResult)
// Get substring from description from index -5 to -1
// Command: getrange description -5 -1
// Result: "sting"
getRangeResult, err = rdb.GetRange(ctx, "description", -5, -1).Result()
if err != nil {
fmt.Println("Command: getrange description -5 -1 | Error: " + err.Error())
}
fmt.Println("Command: getrange description -5 -1 | Result: " + getRangeResult)
// Get substring from description from index 20 to 10
// It will return empty string as the starting index is of a later element
// Command: getrange description 20 10
// Result: ""
getRangeResult, err = rdb.GetRange(ctx, "description", 20, 10).Result()
if err != nil {
fmt.Println("Command: getrange description 20 10 | Error: " + err.Error())
}
fmt.Println("Command: getrange description 20 10 | Result: " + getRangeResult)
// Get substring from description from index -1 to -5
// It will return empty string as the starting index is of a later element
// Command: getrange description -1 -5
// Result: ""
getRangeResult, err = rdb.GetRange(ctx, "description", -1, -5).Result()
if err != nil {
fmt.Println("Command: getrange description -1 -5 | Error: " + err.Error())
}
fmt.Println("Command: getrange description -1 -5 | Result: " + getRangeResult)
// Get substring from description from index 10 to 2000000
// As last index is out of range so the // Result will stop at the end of the source string
// Command: getrange description 10 2000000
// Result: "string for GETRANGE testing"
getRangeResult, err = rdb.GetRange(ctx, "description", 10, 2000000).Result()
if err != nil {
fmt.Println("Command: getrange description 10 2000000 | Error: " + err.Error())
}
fmt.Println("Command: getrange description 10 2000000 | Result: " + getRangeResult)
// Get substring from description from index 5 to 5
// Command: getrange description 5 5
// Result: "l"
getRangeResult, err = rdb.GetRange(ctx, "description", 5, 5).Result()
if err != nil {
fmt.Println("Command: getrange description 5 5 | Error: " + err.Error())
}
fmt.Println("Command: getrange description 5 5 | Result: " + getRangeResult)
// Try to get substring from a key that is not set.
// Returns an empty string.
// Command: getrange wrongkey 10 20
// Result: ""
getRangeResult, err = rdb.GetRange(ctx, "wrongkey", 10, 20).Result()
if err != nil {
fmt.Println("Command: getrange wrongkey 10 20 | Error: " + err.Error())
}
fmt.Println("Command: getrange wrongkey 10 20 | Result: " + getRangeResult)
// Create a list
// Command: lpush mylist abcd
// Result: (integer) 1
listCommandResult, err := rdb.LPush(ctx, "mylist", "abcd").Result()
if err != nil {
fmt.Println("Command: lpush mylist abcd | Error: " + err.Error())
}
fmt.Printf("Command: lpush mylist abcd | Result: %vn", listCommandResult)
// Try to get a substring by index, from the list
// Command: getrange mylist 0 2
// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
getRangeResult, err = rdb.GetRange(ctx, "mylist", 0, 10).Result()
if err != nil {
fmt.Println("Command: getrange mylist 0 2 | Error: " + err.Error())
}
fmt.Println("Command: getrange mylist 0 2 | Result: " + getRangeResult)
}
Output:
Command: set description "some long string for GETRANGE testing" | Result: OK
Command: getrange description 0 10 | Result: some long s
Command: getrange description 0 1 | Result: so
Command: getrange description 0 -1 | Result: some long string for GETRANGE testing
Command: getrange description 20 -1 | Result: GETRANGE testing
Command: getrange description -5 -1 | Result: sting
Command: getrange description 20 10 | Result:
Command: getrange description -1 -5 | Result:
Command: getrange description 10 2000000 | Result: string for GETRANGE testing
Command: getrange description 5 5 | Result: l
Command: getrange wrongkey 10 20 | Result:
Command: lpush mylist abcd | Result: 1
Command: getrange mylist 0 2 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: getrange mylist 0 2 | Result:
Notes
- Use “GetRange” method from redis-go module.
- Signature of the method is-
GetRange(ctx context.Context, key string, start, end int64) *StringCmd
// Redis GETRANGE command example in JavaScript(NodeJS)
import { createClient } from 'redis';
// Create redis client
const redisClient = createClient({
url: 'redis://default:@localhost:6379'
});
redisClient.on('error', err => console.log('Error while connecting to Redis', err));
// Connect Redis client
await redisClient.connect();
/**
* Set some string value for description key
*
* Command: set description "some long string for GETRANGE testing"
* Result: OK
*/
let commandResult = await redisClient.set("description", "some long string for GETRANGE testing");
console.log("Command: set description "some long string for GETRANGE testing" | Result: " + commandResult);
/**
* Get substring from description from index 0 to 10
*
* Command: getrange description 0 10
* Result: "some long s"
*/
commandResult = await redisClient.getRange("description", 0, 10);
console.log("Command: getrange description 0 10 | Result: " + commandResult);
/**
* Get substring from description from index 0 to 1
*
* Command: getrange description 0 1
* Result: "so"
*/
commandResult = await redisClient.getRange("description", 0, 1);
console.log("Command: getrange description 0 1 | Result: " + commandResult);
/**
* Get substring from description from index 0 to -1
*
* Command: getrange description 0 -1
* Result: "some long string for GETRANGE testing"
*/
commandResult = await redisClient.getRange("description", 0, -1);
console.log("Command: getrange description 0 -1 | Result: " + commandResult);
/**
* Get substring from description from index 20 to -1
*
* Command: getrange description 20 -1
* Result: " GETRANGE testing"
*/
commandResult = await redisClient.getRange("description", 20, -1);
console.log("Command: getrange description 20 -1 | Result: " + commandResult);
/**
* Get substring from description from index -5 to -1
* Command: getrange description -5 -1
* Result: "sting"
*/
commandResult = await redisClient.getRange("description", -5, -1);
console.log("Command: getrange description -5 -1 | Result: " + commandResult);
/**
* Get substring from description from index 20 to 10
* It will return empty string as the starting index is of a later element
* Command: getrange description 20 10
* Result: ""
*/
commandResult = await redisClient.getRange("description", 20, 10);
console.log("Command: getrange description 20 10 | Result: " + commandResult);
/**
* Get substring from description from index -1 to -5
* It will return empty string as the starting index is of a later element
* Command: getrange description -1 -5
* Result: ""
*/
commandResult = await redisClient.getRange("description", -1, -5);
console.log("Command: getrange description -1 -5 | Result: " + commandResult);
/**
* Get substring from description from index 10 to 2000000
* As last index is out of range so the * Result will stop at the end of the source string
* Command: getrange description 10 2000000
* Result: "string for GETRANGE testing"
*/
commandResult = await redisClient.getRange("description", 10, 2000000);
console.log("Command: getrange description 10 2000000 | Result: " + commandResult);
/**
* Get substring from description from index 5 to 5
* Command: getrange description 5 5
* Result: "l"
*/
commandResult = await redisClient.getRange("description", 5, 5);
console.log("Command: getrange description 5 5 | Result: " + commandResult);
/**
* Try to get substring from a key that is not set.
* Returns an empty string.
* Command: getrange wrongkey 10 20
* Result: ""
*/
commandResult = await redisClient.getRange("wrongkey", 10, 20);
console.log("Command: getrange wrongkey 10 20 | Result: " + commandResult);
/**
* Create a list
* Command: lpush mylist abcd
* Result: (integer) 1
*/
commandResult = await redisClient.lPush("mylist", "abcd");
console.log("Command: lpush mylist abcd | Result: " + commandResult);
/**
* Try to get a substring by index, from the list
* Command: getrange mylist 0 2
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.getRange("mylist", 0, 10);
console.log("Command: getrange mylist 0 2 | Result: " + commandResult);
} catch (e) {
console.log("Command: getrange mylist 0 2 | Error: " + e);
}
process.exit(0);
Output:
Command: set description "some long string for GETRANGE testing" | Result: OK
Command: getrange description 0 10 | Result: some long s
Command: getrange description 0 1 | Result: so
Command: getrange description 0 -1 | Result: some long string for GETRANGE testing
Command: getrange description 20 -1 | Result: GETRANGE testing
Command: getrange description -5 -1 | Result: sting
Command: getrange description 20 10 | Result:
Command: getrange description -1 -5 | Result:
Command: getrange description 10 2000000 | Result: string for GETRANGE testing
Command: getrange description 5 5 | Result: l
Command: getrange wrongkey 10 20 | Result:
Command: lpush mylist abcd | Result: 1
Command: getrange mylist 0 2 | Error: Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “getRange” from the package node-redis.
// Redis GETRANGE Command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class Getrange {
public static void main(String[] args) {
// Create connection pool
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Set some string value for description key
*
* Command: set description "some long string for GETRANGE testing"
* Result: OK
*/
String setResult = jedis.set("description", "some long string for GETRANGE testing");
System.out.println("Command: set description "some long string for GETRANGE testing" | Result: " + setResult);
/**
* Get substring from description from index 0 to 10
*
* Command: getrange description 0 10
* Result: "some long s"
*/
String getRangeResult = jedis.getrange("description", 0, 10);
System.out.println("Command: getrange description 0 10 | Result: " + getRangeResult);
/**
* Get substring from description from index 0 to 1
*
* Command: getrange description 0 1
* Result: "so"
*/
getRangeResult = jedis.getrange("description", 0, 1);
System.out.println("Command: getrange description 0 1 | Result: " + getRangeResult);
/**
* Get substring from description from index 0 to -1
*
* Command: getrange description 0 -1
* Result: "some long string for GETRANGE testing"
*/
getRangeResult = jedis.getrange("description", 0, -1);
System.out.println("Command: getrange description 0 -1 | Result: " + getRangeResult);
/**
* Get substring from description from index 20 to -1
*
* Command: getrange description 20 -1
* Result: " GETRANGE testing"
*/
getRangeResult = jedis.getrange("description", 20, -1);
System.out.println("Command: getrange description 20 -1 | Result: " + getRangeResult);
/**
* Get substring from description from index -5 to -1
* Command: getrange description -5 -1
* Result: "sting"
*/
getRangeResult = jedis.getrange("description", -5, -1);
System.out.println("Command: getrange description -5 -1 | Result: " + getRangeResult);
/**
* Get substring from description from index 20 to 10
* It will return empty string as the starting index is of a later element
* Command: getrange description 20 10
* Result: ""
*/
getRangeResult = jedis.getrange("description", 20, 10);
System.out.println("Command: getrange description 20 10 | Result: " + getRangeResult);
/**
* Get substring from description from index -1 to -5
* It will return empty string as the starting index is of a later element
* Command: getrange description -1 -5
* Result: ""
*/
getRangeResult = jedis.getrange("description", -1, -5);
System.out.println("Command: getrange description -1 -5 | Result: " + getRangeResult);
/**
* Get substring from description from index 10 to 2000000
* As last index is out of range so the * Result will stop at the end of the source string
* Command: getrange description 10 2000000
* Result: "string for GETRANGE testing"
*/
getRangeResult = jedis.getrange("description", 10, 2000000);
System.out.println("Command: getrange description 10 2000000 | Result: " + getRangeResult);
/**
* Get substring from description from index 5 to 5
* Command: getrange description 5 5
* Result: "l"
*/
getRangeResult = jedis.getrange("description", 5, 5);
System.out.println("Command: getrange description 5 5 | Result: " + getRangeResult);
/**
* Try to get substring from a key that is not set.
* Returns an empty string.
* Command: getrange wrongkey 10 20
* Result: ""
*/
getRangeResult = jedis.getrange("wrongkey", 10, 20);
System.out.println("Command: getrange wrongkey 10 20 | Result: " + getRangeResult);
/**
* Create a list
* Command: lpush mylist abcd
* Result: (integer) 1
*/
long listCommandResult = jedis.lpush("mylist", "abcd");
System.out.println("Command: lpush mylist abcd | Result: " + listCommandResult);
/**
* Try to get a substring by index, from the list
* Command: getrange mylist 0 2
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
getRangeResult = jedis.getrange("mylist", 0, 10);
System.out.println("Command: getrange mylist 0 2 | Result: " + getRangeResult);
} catch (Exception e) {
System.out.println("Command: getrange mylist 0 2 | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: set description "some long string for GETRANGE testing" | Result: OK
Command: getrange description 0 10 | Result: some long s
Command: getrange description 0 1 | Result: so
Command: getrange description 0 -1 | Result: some long string for GETRANGE testing
Command: getrange description 20 -1 | Result: GETRANGE testing
Command: getrange description -5 -1 | Result: sting
Command: getrange description 20 10 | Result:
Command: getrange description -1 -5 | Result:
Command: getrange description 10 2000000 | Result: string for GETRANGE testing
Command: getrange description 5 5 | Result: l
Command: getrange wrongkey 10 20 | Result:
Command: lpush mylist abcd | Result: 1
Command: getrange mylist 0 2 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “getrange” from Jedis package.
- Signature of the “getrange” is-
public String getrange(final String key, final long startOffset, final long endOffset)
// Redis GETRANGE command examples in C#
using StackExchange.Redis;
namespace Getrange
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Set some string value for description key
*
* Command: set description "some long string for GETRANGE testing"
* Result: OK
*/
bool setResult = rdb.StringSet("description", "some long string for GETRANGE testing");
Console.WriteLine("Command: set description "some long string for GETRANGE testing" | Result: " + setResult);
/**
* Get substring from description from index 0 to 10
*
* Command: getrange description 0 10
* Result: "some long s"
*/
RedisValue getRangeResult = rdb.StringGetRange("description", 0, 10);
Console.WriteLine("Command: getrange description 0 10 | Result: " + getRangeResult);
/**
* Get substring from description from index 0 to 1
*
* Command: getrange description 0 1
* Result: "so"
*/
getRangeResult = rdb.StringGetRange("description", 0, 1);
Console.WriteLine("Command: getrange description 0 1 | Result: " + getRangeResult);
/**
* Get substring from description from index 0 to -1
*
* Command: getrange description 0 -1
* Result: "some long string for GETRANGE testing"
*/
getRangeResult = rdb.StringGetRange("description", 0, -1);
Console.WriteLine("Command: getrange description 0 -1 | Result: " + getRangeResult);
/**
* Get substring from description from index 20 to -1
*
* Command: getrange description 20 -1
* Result: " GETRANGE testing"
*/
getRangeResult = rdb.StringGetRange("description", 20, -1);
Console.WriteLine("Command: getrange description 20 -1 | Result: " + getRangeResult);
/**
* Get substring from description from index -5 to -1
* Command: getrange description -5 -1
* Result: "sting"
*/
getRangeResult = rdb.StringGetRange("description", -5, -1);
Console.WriteLine("Command: getrange description -5 -1 | Result: " + getRangeResult);
/**
* Get substring from description from index 20 to 10
* It will return empty string as the starting index is of a later element
* Command: getrange description 20 10
* Result: ""
*/
getRangeResult = rdb.StringGetRange("description", 20, 10);
Console.WriteLine("Command: getrange description 20 10 | Result: " + getRangeResult);
/**
* Get substring from description from index -1 to -5
* It will return empty string as the starting index is of a later element
* Command: getrange description -1 -5
* Result: ""
*/
getRangeResult = rdb.StringGetRange("description", -1, -5);
Console.WriteLine("Command: getrange description -1 -5 | Result: " + getRangeResult);
/**
* Get substring from description from index 10 to 2000000
* As last index is out of range so the * Result will stop at the end of the source string
* Command: getrange description 10 2000000
* Result: "string for GETRANGE testing"
*/
getRangeResult = rdb.StringGetRange("description", 10, 2000000);
Console.WriteLine("Command: getrange description 10 2000000 | Result: " + getRangeResult);
/**
* Get substring from description from index 5 to 5
* Command: getrange description 5 5
* Result: "l"
*/
getRangeResult = rdb.StringGetRange("description", 5, 5);
Console.WriteLine("Command: getrange description 5 5 | Result: " + getRangeResult);
/**
* Try to get substring from a key that is not set.
* Returns an empty string.
* Command: getrange wrongkey 10 20
* Result: ""
*/
getRangeResult = rdb.StringGetRange("wrongkey", 10, 20);
Console.WriteLine("Command: getrange wrongkey 10 20 | Result: " + getRangeResult);
/**
* Create a list
* Command: lpush mylist abcd
* Result: (integer) 1
*/
long listCommandResult = rdb.ListLeftPush("mylist", "abcd");
Console.WriteLine("Command: lpush mylist abcd | Result: " + listCommandResult);
/**
* Try to get a substring by index, from the list
* Command: getrange mylist 0 2
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
getRangeResult = rdb.StringGetRange("mylist", 0, 10);
Console.WriteLine("Command: getrange mylist 0 2 | Result: " + getRangeResult);
}
catch (Exception e)
{
Console.WriteLine("Command: getrange mylist 0 2 | Error: " + e.Message);
}
}
}
}
Output:
Command: set description "some long string for GETRANGE testing" | Result: True
Command: getrange description 0 10 | Result: some long s
Command: getrange description 0 1 | Result: so
Command: getrange description 0 -1 | Result: some long string for GETRANGE testing
Command: getrange description 20 -1 | Result: GETRANGE testing
Command: getrange description -5 -1 | Result: sting
Command: getrange description 20 10 | Result:
Command: getrange description -1 -5 | Result:
Command: getrange description 10 2000000 | Result: string for GETRANGE testing
Command: getrange description 5 5 | Result: l
Command: getrange wrongkey 10 20 | Result:
Command: lpush mylist abcd | Result: 1
Command: getrange mylist 0 2 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “StringGetRange” from StackExchange.Redis.
- Signature of the method is-
RedisValue StringGetRange(RedisKey key, long start, long end, CommandFlags flags = CommandFlags.None)
<?php
// Redis GETRANGE command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Set some string value for description key
*
* Command: set description "some long string for GETRANGE testing"
* Result: OK
*/
$commandResult = $redisClient->set("description", "some long string for GETRANGE testing");
echo "Command: set description "some long string for GETRANGE testing" | Result: " . $commandResult . "n";
/**
* Get substring from description from index 0 to 10
*
* Command: getrange description 0 10
* Result: "some long s"
*/
$commandResult = $redisClient->getrange("description", 0, 10);
echo "Command: getrange description 0 10 | Result: " . $commandResult . "n";
/**
* Get substring from description from index 0 to 1
*
* Command: getrange description 0 1
* Result: "so"
*/
$commandResult = $redisClient->getrange("description", 0, 1);
echo "Command: getrange description 0 1 | Result: " . $commandResult . "n";
/**
* Get substring from description from index 0 to -1
*
* Command: getrange description 0 -1
* Result: "some long string for GETRANGE testing"
*/
$commandResult = $redisClient->getrange("description", 0, -1);
echo "Command: getrange description 0 -1 | Result: " . $commandResult . "n";
/**
* Get substring from description from index 20 to -1
*
* Command: getrange description 20 -1
* Result: " GETRANGE testing"
*/
$commandResult = $redisClient->getrange("description", 20, -1);
echo "Command: getrange description 20 -1 | Result: " . $commandResult . "n";
/**
* Get substring from description from index -5 to -1
* Command: getrange description -5 -1
* Result: "sting"
*/
$commandResult = $redisClient->getrange("description", -5, -1);
echo "Command: getrange description -5 -1 | Result: " . $commandResult . "n";
/**
* Get substring from description from index 20 to 10
* It will return empty string as the starting index is of a later element
* Command: getrange description 20 10
* Result: ""
*/
$commandResult = $redisClient->getrange("description", 20, 10);
echo "Command: getrange description 20 10 | Result: " . $commandResult . "n";
/**
* Get substring from description from index -1 to -5
* It will return empty string as the starting index is of a later element
* Command: getrange description -1 -5
* Result: ""
*/
$commandResult = $redisClient->getrange("description", -1, -5);
echo "Command: getrange description -1 -5 | Result: " . $commandResult . "n";
/**
* Get substring from description from index 10 to 2000000
* As last index is out of range so the * Result will stop at the end of the source string
* Command: getrange description 10 2000000
* Result: "string for GETRANGE testing"
*/
$commandResult = $redisClient->getrange("description", 10, 2000000);
echo "Command: getrange description 10 2000000 | Result: " . $commandResult . "n";
/**
* Get substring from description from index 5 to 5
* Command: getrange description 5 5
* Result: "l"
*/
$commandResult = $redisClient->getrange("description", 5, 5);
echo "Command: getrange description 5 5 | Result: " . $commandResult . "n";
/**
* Try to get substring from a key that is not set.
* Returns an empty string.
* Command: getrange wrongkey 10 20
* Result: ""
*/
$commandResult = $redisClient->getrange("wrongkey", 10, 20);
echo "Command: getrange wrongkey 10 20 | Result: " . $commandResult . "n";
/**
* Create a list
* Command: lpush mylist abcd
* Result: (integer) 1
*/
$listCommandResult = $redisClient->lpush("mylist", ["abcd"]);
echo "Command: lpush mylist abcd | Result: " . $listCommandResult . "n";
/**
* Try to get a substring by index, from the list
* Command: getrange mylist 0 2
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->getrange("mylist", 0, 10);
echo "Command: getrange mylist 0 2 | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: getrange mylist 0 2 | Error: " . $e->getMessage() . "n";
}
Output:
Command: set description "some long string for GETRANGE testing" | Result: OK
Command: getrange description 0 10 | Result: some long s
Command: getrange description 0 1 | Result: so
Command: getrange description 0 -1 | Result: some long string for GETRANGE testing
Command: getrange description 20 -1 | Result: GETRANGE testing
Command: getrange description -5 -1 | Result: sting
Command: getrange description 20 10 | Result:
Command: getrange description -1 -5 | Result:
Command: getrange description 10 2000000 | Result: string for GETRANGE testing
Command: getrange description 5 5 | Result: l
Command: getrange wrongkey 10 20 | Result:
Command: lpush mylist abcd | Result: 1
Command: getrange mylist 0 2 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “getrange” of predis.
- Signature of the method is-
getrange(string $key, $start, $end): string
# Redis GETRANGE command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
username='default', password='',
decode_responses=True)
# Set some string value for description key
# Command: set description "some long string for GETRANGE testing"
# Result: OK
commandResult = redisClient.set(
"description", "some long string for GETRANGE testing")
print("Command: set description "some long string for GETRANGE testing" | Result: {}".format(commandResult))
# Get substring from description from index 0 to 10
# Command: getrange description 0 10
# Result: "some long s"
commandResult = redisClient.getrange("description", 0, 10)
print("Command: getrange description 0 10 | Result: {}".format(commandResult))
# Get substring from description from index 0 to 1
# Command: getrange description 0 1
# Result: "so"
commandResult = redisClient.getrange("description", 0, 1)
print("Command: getrange description 0 1 | Result: {}".format(commandResult))
# Get substring from description from index 0 to -1
# Command: getrange description 0 -1
# Result: "some long string for GETRANGE testing"
commandResult = redisClient.getrange("description", 0, -1)
print("Command: getrange description 0 -1 | Result: {}".format(commandResult))
# Get substring from description from index 20 to -1
# Command: getrange description 20 -1
# Result: " GETRANGE testing"
commandResult = redisClient.getrange("description", 20, -1)
print("Command: getrange description 20 -1 | Result: {}".format(commandResult))
# Get substring from description from index -5 to -1
# Command: getrange description -5 -1
# Result: "sting"
commandResult = redisClient.getrange("description", -5, -1)
print("Command: getrange description -5 -1 | Result: {}".format(commandResult))
# Get substring from description from index 20 to 10
# It will return empty string as the starting index is of a later element
# Command: getrange description 20 10
# Result: ""
commandResult = redisClient.getrange("description", 20, 10)
print("Command: getrange description 20 10 | Result: {}".format(commandResult))
# Get substring from description from index -1 to -5
# It will return empty string as the starting index is of a later element
# Command: getrange description -1 -5
# Result: ""
commandResult = redisClient.getrange("description", -1, -5)
print("Command: getrange description -1 -5 | Result: {}".format(commandResult))
# Get substring from description from index 10 to 2000000
# As last index is out of range so the # Result will stop at the end of the source string
# Command: getrange description 10 2000000
# Result: "string for GETRANGE testing"
commandResult = redisClient.getrange("description", 10, 2000000)
print("Command: getrange description 10 2000000 | Result: {}".format(commandResult))
# Get substring from description from index 5 to 5
# Command: getrange description 5 5
# Result: "l"
commandResult = redisClient.getrange("description", 5, 5)
print("Command: getrange description 5 5 | Result: {}".format(commandResult))
# Try to get substring from a key that is not set.
# Returns an empty string.
# Command: getrange wrongkey 10 20
# Result: ""
commandResult = redisClient.getrange("wrongkey", 10, 20)
print("Command: getrange wrongkey 10 20 | Result: {}".format(commandResult))
# Create a list
# Command: lpush mylist abcd
# Result: (integer) 1
listCommandResult = redisClient.lpush("mylist", "abcd")
print("Command: lpush mylist abcd | Result: {}".format(listCommandResult))
# Try to get a substring by index, from the list
# Command: getrange mylist 0 2
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.getrange("mylist", 0, 10)
print("Command: getrange mylist 0 2 | Result: {}".format(commandResult))
except Exception as error:
print("Command: getrange mylist 0 2 | Error: ", error)
Output:
Command: set description "some long string for GETRANGE testing" | Result: True
Command: getrange description 0 10 | Result: some long s
Command: getrange description 0 1 | Result: so
Command: getrange description 0 -1 | Result: some long string for GETRANGE testing
Command: getrange description 20 -1 | Result: GETRANGE testing
Command: getrange description -5 -1 | Result: sting
Command: getrange description 20 10 | Result:
Command: getrange description -1 -5 | Result:
Command: getrange description 10 2000000 | Result: string for GETRANGE testing
Command: getrange description 5 5 | Result: l
Command: getrange wrongkey 10 20 | Result:
Command: lpush mylist abcd | Result: 1
Command: getrange mylist 0 2 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “getrange” from redis-py.
- Signature of the method is –
def getrange(self, key: KeyT, start: int, end: int) -> ResponseT
# Redis GETRANGE command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Set some string value for description key
# Command: set description "some long string for GETRANGE testing"
# Result: OK
commandResult = redis.set("description", "some long string for GETRANGE testing")
print("Command: set description "some long string for GETRANGE testing" | Result: ", commandResult, "n")
# Get substring from description from index 0 to 10
# Command: getrange description 0 10
# Result: "some long s"
commandResult = redis.getrange("description", 0, 10)
print("Command: getrange description 0 10 | Result: ", commandResult, "n")
# Get substring from description from index 0 to 1
# Command: getrange description 0 1
# Result: "so"
commandResult = redis.getrange("description", 0, 1)
print("Command: getrange description 0 1 | Result: ", commandResult, "n")
# Get substring from description from index 0 to -1
# Command: getrange description 0 -1
# Result: "some long string for GETRANGE testing"
commandResult = redis.getrange("description", 0, -1)
print("Command: getrange description 0 -1 | Result: ", commandResult, "n")
# Get substring from description from index 20 to -1
# Command: getrange description 20 -1
# Result: " GETRANGE testing"
commandResult = redis.getrange("description", 20, -1)
print("Command: getrange description 20 -1 | Result: ", commandResult, "n")
# Get substring from description from index -5 to -1
# Command: getrange description -5 -1
# Result: "sting"
commandResult = redis.getrange("description", -5, -1)
print("Command: getrange description -5 -1 | Result: ", commandResult, "n")
# Get substring from description from index 20 to 10
# It will return empty string as the starting index is of a later element
# Command: getrange description 20 10
# Result: ""
commandResult = redis.getrange("description", 20, 10)
print("Command: getrange description 20 10 | Result: ", commandResult, "n")
# Get substring from description from index -1 to -5
# It will return empty string as the starting index is of a later element
# Command: getrange description -1 -5
# Result: ""
commandResult = redis.getrange("description", -1, -5)
print("Command: getrange description -1 -5 | Result: ", commandResult, "n")
# Get substring from description from index 10 to 2000000
# As last index is out of range so the # Result will stop at the end of the source string
# Command: getrange description 10 2000000
# Result: "string for GETRANGE testing"
commandResult = redis.getrange("description", 10, 2000000)
print("Command: getrange description 10 2000000 | Result: ", commandResult, "n")
# Get substring from description from index 5 to 5
# Command: getrange description 5 5
# Result: "l"
commandResult = redis.getrange("description", 5, 5)
print("Command: getrange description 5 5 | Result: ", commandResult, "n")
# Try to get substring from a key that is not set.
# Returns an empty string.
# Command: getrange wrongkey 10 20
# Result: ""
commandResult = redis.getrange("wrongkey", 10, 20)
print("Command: getrange wrongkey 10 20 | Result: ", commandResult, "n")
# Create a list
# Command: lpush mylist abcd
# Result: (integer) 1
listCommandResult = redis.lpush("mylist", "abcd")
print("Command: lpush mylist abcd | Result: ", listCommandResult, "n")
# Try to get a substring by index, from the list
# Command: getrange mylist 0 2
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.getrange("mylist", 0, 10)
print("Command: getrange mylist 0 2 | Result: ", commandResult, "n")
rescue => e
print("Command: getrange mylist 0 2 | Error: ", e, "n")
end
Output:
Command: set description "some long string for GETRANGE testing" | Result: OK
Command: getrange description 0 10 | Result: some long s
Command: getrange description 0 1 | Result: so
Command: getrange description 0 -1 | Result: some long string for GETRANGE testing
Command: getrange description 20 -1 | Result: GETRANGE testing
Command: getrange description -5 -1 | Result: sting
Command: getrange description 20 10 | Result:
Command: getrange description -1 -5 | Result:
Command: getrange description 10 2000000 | Result: string for GETRANGE testing
Command: getrange description 5 5 | Result: l
Command: getrange wrongkey 10 20 | Result:
Command: lpush mylist abcd | Result: 4
Command: getrange mylist 0 2 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “getrange” from the redis-rb.
- Signature of the “getrange” method is-
def getrange(key, start, stop)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
SUBSTR | Command Details |
STRLEN | Command Details |
LCS | Command Details |