Golang bytes.Trim() function example

package bytes

Trim returns a subslice of the input slice by slicing off all leading and trailing UTF-8-encoded Unicode code points contained in cut set (2nd parameter).

Golang bytes.Trim() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {
 str := []byte("Hi! How are you ? no")

 trimmed := bytes.Trim(str, "Hi!no") // leading and trailing
 fmt.Println(string(trimmed))

 num := []byte("123456789")

 trimmed2 := bytes.Trim(num, "1789") // leading and trailing

 fmt.Println(string(trimmed2))
 }

Output :

How are you ?

23456

http://golang.org/pkg/bytes/#Trim

Advertisement