GETRANGE(3valkey)
π getrange β okay bestie, let's talk substrings π
So this is GETRANGE, a command from the Valkey Command Manual (that's the 3valkey manual, section 3, the library/command-call section, iykyk π«Ά). It returns a substring of the string stored at a key. That's it. That's the vibe. Slay.
β¨ SYNOPSIS β¨
GETRANGE key start end
πΈ DESCRIPTION πΈ
GETRANGE gives you back the substring of the string value sitting at key, and you pick which chunk you want with the offsets start and end. Heads up: both of those are inclusive, so end is genuinely included, not a "stop before this" situation. Inclusive. Both of them. Periodt. π
And the cute part? You can use negative offsets, which count backwards from the end of the string instead of the front. So:
-1is literally the last character-2is the penultimate one- and so on and so forth, all the way down the line
If you ask for something that's out of range, don't panic β the function just handles it for you by limiting the resulting range to the actual length of the string. No errors, no drama, just a smaller substring than you asked for. It's giving "we'll work with what we have." π
π¬ REPLY π¬
A bulk string reply in the valkey-protocol(7) sense β specifically, the substring of the string value stored at key, determined by the offsets start and end (both inclusive, again, always inclusive).
π₯ COMPLEXITY π₯
It's O(N), where N is the length of the returned string.
Okay but here's the nuance, bestie: the complexity is ultimately determined by the returned length. However β and this is the fun bit β because creating a substring from an existing string is very cheap, it can be considered O(1) for small strings. So it's not scary, it's just honest about scaling. β¨
π ACL CATEGORIES π
@read @slow @string
π¦ HISTORY π¦
- Available since: 2.4.0 β an elder, an icon, a legend, no cap.
π EXAMPLES π
127.0.0.1:6379> SET mykey "This is a string"
OK
127.0.0.1:6379> GETRANGE mykey 0 3
"This"
127.0.0.1:6379> GETRANGE mykey -3 -1
"ing"
127.0.0.1:6379> GETRANGE mykey 0 -1
"This is a string"
127.0.0.1:6379> GETRANGE mykey 10 100
"string"
Look at that last one π₯Ή β asked for 10 to 100 on a string that's way shorter than that, and it just quietly gave back "string". Out-of-range handling, main character energy.
π SEE ALSO π
append(3valkey), decr(3valkey), decrby(3valkey), delifeq(3valkey), get(3valkey), getdel(3valkey), getex(3valkey), getset(3valkey), incr(3valkey), incrby(3valkey), incrbyfloat(3valkey), lcs(3valkey), mget(3valkey), mset(3valkey), msetnx(3valkey), psetex(3valkey), set(3valkey), setex(3valkey), setnx(3valkey), setrange(3valkey), strlen(3valkey), substr(3valkey)
Okay that's the whole page, bestie!! π This was version 9.0.0, dated 2025-10-21, straight from the GETRANGE(3valkey) manual. Go forth and slice your strings, you absolute legend. π«Άβ¨
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms