trim
Removes leading or trailing zero bytes/characters from a byte slice or hex string
trim
Removes leading or trailing zero bytes/characters from a byte slice or hex string.
Import
import "github.com/ChefBingbong/viem-go/utils/data"
Usage
import "github.com/ChefBingbong/viem-go/utils/data"// Trim leading zeros from bytesresult := data.TrimBytes([]byte{0x00, 0x00, 0x01, 0x02}, data.TrimLeft)// []byte{0x01, 0x02}// Trim trailing zeros from bytesresult := data.TrimBytes([]byte{0x01, 0x02, 0x00, 0x00}, data.TrimRight)// []byte{0x01, 0x02}// Trim leading zeros from hex stringhexResult := data.TrimHex("0x00000102", data.TrimLeft)// "0x0102"// Trim trailing zeros from hex stringhexResult := data.TrimHex("0x01020000", data.TrimRight)// "0x0102"
Returns
- Type:
[]byteforTrimBytes,stringforTrimHex
The trimmed value with leading or trailing zeros removed.
Parameters
value (required)
- Type:
[]byteforTrimBytes,stringforTrimHex
The byte slice or hex string to trim.
result := data.TrimBytes([]byte{0x00, 0x00, 0x01, 0x02}, data.TrimLeft)
dir (required)
- Type:
data.TrimDirection(TrimLeftorTrimRight)
The direction in which to trim zeros – either leading (left) or trailing (right).
// Trim leading zerosresult := data.TrimBytes([]byte{0x00, 0x00, 0x01, 0x02}, data.TrimLeft)// []byte{0x01, 0x02}// Trim trailing zerosresult := data.TrimBytes([]byte{0x01, 0x02, 0x00, 0x00}, data.TrimRight)// []byte{0x01, 0x02}// Trim hex leading zeroshexResult := data.TrimHex("0x00000102", data.TrimLeft)// "0x0102"// Trim hex trailing zeroshexResult := data.TrimHex("0x01020000", data.TrimRight)// "0x0102"
Convenience Functions
TrimLeftBytes / TrimRightBytes
Convenience functions for trimming bytes in a specific direction:
result := data.TrimLeftBytes([]byte{0x00, 0x00, 0x01, 0x02})
// []byte{0x01, 0x02}
result := data.TrimRightBytes([]byte{0x01, 0x02, 0x00, 0x00})
// []byte{0x01, 0x02}
TrimLeftHex / TrimRightHex
Convenience functions for trimming hex strings in a specific direction:
result := data.TrimLeftHex("0x00000102")
// "0x0102"
result := data.TrimRightHex("0x01020000")
// "0x0102"