Open In App

How to trim a slice of bytes in Golang?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to trim all the leading and trailing UTF-8-encoded code points from the given slice using Trim() function. This function returns a subslice of the original slice by slicing off all leading and trailing UTF-8-encoded code points which are specified in the given string. If the given slice of bytes does not contain the specified string in it, then this function returns the original slice without any change. It is defined under the bytes package so, you have to import bytes package in your program for accessing Trim function. 

Syntax: 

func Trim(ori_slice[]byte, cut_string string) []byte

Here, ori_slice is the original slice of bytes and cut_string represent a string which you want to trim in the given slice. Let us discuss this concept with the help of the given examples: Example 1: 

Go




// Go program to illustrate the concept
// of trim in the slice of bytes
package main
 
import (
    "bytes"
    "fmt"
)
 
func main() {
 
    // Creating and initializing
    // the slice of bytes
    // Using shorthand declaration
    slice_1 := []byte{'!', '!', 'G', 'e', 'e', 'k', 's', 'f'
                 'o', 'r', 'G', 'e', 'e', 'k', 's', '#', '#'}
     
    slice_2 := []byte{'*', '*', 'A', 'p', 'p', 'l', 'e', '^', '^'}
     
    slice_3 := []byte{'%', 'g', 'e', 'e', 'k', 's', '%'}
 
    // Displaying slices
    fmt.Println("Original Slice:")
    fmt.Printf("Slice 1: %s", slice_1)
    fmt.Printf("\nSlice 2: %s", slice_2)
    fmt.Printf("\nSlice 3: %s", slice_3)
 
    // Trimming specified leading
    // and trailing Unicodes points
    // from the given slice of bytes
    // Using Trim function
    res1 := bytes.Trim(slice_1, "!#")
    res2 := bytes.Trim(slice_2, "*^")
    res3 := bytes.Trim(slice_3, "@")
 
    // Display the results
    fmt.Printf("New Slice:\n")
    fmt.Printf("\nSlice 1: %s", res1)
    fmt.Printf("\nSlice 2: %s", res2)
    fmt.Printf("\nSlice 3: %s", res3)
 
}


Output:

Original Slice:
Slice 1: !!GeeksforGeeks##
Slice 2: **Apple^^
Slice 3: %geeks%New Slice:

Slice 1: GeeksforGeeks
Slice 2: Apple
Slice 3: %geeks%

Example 2: 

Go




// Go program to illustrate the concept
// of trim in the slice of bytes
package main
 
import (
    "bytes"
    "fmt"
)
 
func main() {
 
    // Creating and trimming
    // the slice of bytes
    // Using Trim function
    res1 := bytes.Trim([]byte("****Welcome to GeeksforGeeks****"), "*")
    res2 := bytes.Trim([]byte("!!!!Learning how to trim a slice of bytes@@@@"), "!@")
    res3 := bytes.Trim([]byte("^^Geek&&"), "$")
 
    // Display the results
    fmt.Printf("Final Slice:\n")
    fmt.Printf("\nSlice 1: %s", res1)
    fmt.Printf("\nSlice 2: %s", res2)
    fmt.Printf("\nSlice 3: %s", res3)
}


Output:

Final Slice:

Slice 1: Welcome to GeeksforGeeks
Slice 2: Learning how to trim a slice of bytes
Slice 3: ^^Geek&&

Another approach:  Using the bytes.Trim function

We can trim a slice of bytes using the bytes.Trim function from the bytes package. The bytes.Trim function takes two arguments: a slice of bytes and a string containing one or more characters to trim from the beginning and end of the slice. The function returns a new slice that contains the original slice with all instances of the specified characters trimmed from the beginning and end.

Note that the bytes.Trim function can be used to trim any set of characters, not just whitespace characters. To trim a specific set of characters, simply pass a string containing those characters as the second argument to the bytes.

Go




package main
 
import (
    "bytes"
    "fmt"
)
 
func main() {
    byteSlice := []byte("  Hello, World!  ")
    trimmed := bytes.TrimSpace(byteSlice)
    fmt.Println(string(trimmed)) // "Hello, World!"
}


Output:

Hello, World!

Time Complexity: O(n)
Auxiliary space: O(n)



Last Updated : 23 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads