GETRANGE(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– 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:

  • -1 is literally the last character
  • -2 is 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