Trimming in haskell

I’m wanting to trim a predetermined length of a bytestring from either the start or end. I’ve been able to see a lot of how to do this for whitespace, but how would I do this with a length and position?

edit: added haskell tag

1 Like

You can split a string on any delimiter not just whitespace however it sounds like you want a substring based on length. In which case something like this ought to work:

import Data.ByteString as XS
import Data.Int

substr :: Int -> Int -> ByteString -> ByteString
substr off len = XS.take len . XS.drop off

take returns the prefix and drop returns the suffix. So take 1 "hello" would return “h” and drop 4 "hello" would return “o” and we can combine these to make a substring function. Also the alias XS here is to avoid ambiguity with PlutusTx.Prelude version of 'take but may not be needed. Of course rename things until they make sense to you …

Usage: substr 8 10 "I guess this works well enough" returns "this works"

Reference: Data.ByteString