Golang Trim Strings [Left, Right, Space, Suffix, Prefix]


Written by - Antony Shikubu
Reviewed by - Deepak Prasad

In Go, a string is a sequence of characters enclosed between opening and closing double quotes “ ”. Sometimes you might need to remove unwanted characters in a string. The Go string package comes in handy and provides methods that can help trim a string to the desired output. This article will show you how to trim a string using various methods found in the strings package.

 

strings.Trim()

This trim method is used to remove leading and trailing Unicode code points. It returns to the caller a slice of string(s).

Syntax

response := strings.Trim(s string, cut_string string)

The first parameter for the strings.Trim() function is the string that needs to be trimmed. The second parameter is the character string that needs to be trimmed from the first parameter.If the cut string does not exist in the provided string, the strings.Trim() returns the provided string without trimming anything.

Example

package main
 
import (
   "fmt"
   "strings"
)
 
func main() {
   string1 := "$$ Hello world $$"
   string2 := "!! Welcome to the GolinuxCloud Website !!"
   fmt.Println("String before using strings.Trim()")
   fmt.Printf("%s \n", string1)
   fmt.Printf("%s \n", string2)
 
   response1 := strings.Trim(string1, "$$")
   response2 := strings.Trim(string2, "!!")
   fmt.Println("String after using strings.Trim()")
   fmt.Printf("%s \n", response1)
   fmt.Printf("%s \n", response2)
}

Explanation

In the preceding example, we define two strings with leading and string unicode characters. Given a cut string, the strings.Trim() function cuts off the provided cut string from the provided string.

Output

$ go run main.go
String before using strings.Trim()
$$ Hello world $$
!! Welcome to the GolinuxCloud Website !!
String after using strings.Trim()
Hello world 
Welcome to the GolinuxCloud Website

 

strings.TrimLeft()

The strings.TrimLeft() method removes/cuts  leading unicode points contained in the cutset. It returns a slice of strings with the leading unicode pointes removed.

Syntax

response := strings.TrimLeft(s string, cut_string string)

The first parameter in the strings.TrimLeft() function is the string with leading unicode points, and the second parameter is the set of strings that need to be cut from it.

Example

package main
 
import (
   "fmt"
   "strings"
)
 
func main() {
   string1 := "$$ Hello world"
   string2 := "!! Welcome to the GolinuxCloud Website"
 
   fmt.Println("String before using strings.TrimLeft()")
   fmt.Printf("%s \n", string1)
   fmt.Printf("%s \n", string2)
 
   response1 := strings.TrimLeft(string1, "$$")
   response2 := strings.TrimLeft(string2, "!!")
 
   fmt.Println("String after using string.Trim()")
   fmt.Printf("%s \n", response1)
   fmt.Printf("%s \n", response2)
}

Explanation

In the above example, strings.TrimLeft() function is supplied with two parameters, one is the string with leading unicode points, and the other with cut string set. The strings.TrimLeft() method returns a string without the leading unicode points that have been specified in the cut string set as shown in the output.

Output

$ go run main.go
String before using strings.TrimLeft()
$$ Hello world
!! Welcome to the GolinuxCloud Website
String after using strings.TrimLeft()
Hello world
Welcome to the GolinuxCloud Website

 

strings.TrimRight()

The strings.TrimRight() method cuts/removes all trialing unicode points contained in the cutset. It returns a string without the trailing unicode points that were specified in the cutset.

Syntax

response := strings.TrimRight(s string, cut_string string)

The strings.TrimRight() method takes two parameters, one is the string with trailing unicode points, and the second parameter is the cut set.

Example

package main
 
import (
   "fmt"
   "strings"
)
 
func main() {
 
   string1 := "Hello world $$"
   string2 := "Welcome to the GolinuxCloud Website !!"
   string3 := "Hello world ## "
 
   fmt.Println("String before using strings.TrimRight()")
   fmt.Printf("%s \n", string1)
   fmt.Printf("%s \n", string2)
   fmt.Printf("%s \n", string3)
 
   response1 := strings.TrimRight(string1, "$$")
   response2 := strings.TrimRight(string2, "!!")
   response3 := strings.TrimRight(string3, "##")
 
   fmt.Println("String after using strings.TrimRight()")
   fmt.Printf("%s \n", response1)
   fmt.Printf("%s \n", response2)
   fmt.Printf("%s \n", response3)
}

Explanation

In the preceding example, we pass strings with trailing unicode points to the strings.TrimRight() method and cutset. The strings.TrimRight() method removes/cuts all the trailing unicode points contained in the cutset. One thing to take note of in the above example, is string3, it has a white space as the trailing value. Using strings.TrimRight() method will not remove the provided cutset(##) from the string.

Output

$ go run main.go
String before using strings.TrimRight()
Hello world $$
Welcome to the GolinuxCloud Website !!
Hello world ## 
String after using strings.TrimRight()
Hello world 
Welcome to the GolinuxCloud Website 
Hello world ##

 

strings.TrimSpace()

The strings.TrimSpace() method, just like the name suggests, removes trailing and leading white spaces. It returns a string without leading and trailing spaces.

Syntax

response := strings.TrimSpace(s string)

The strings.TrimSpace() method takes one parameter, which is the string with either leading or trailing white spaces.

Example

package main
 
import (
   "fmt"
   "strings"
)
 
func main() {
   string1 := "  Hello world"
   string2 := "Welcome to the GolinuxCloud Website "
   string3 := " $$ Hello world ## "
 
   fmt.Println("String before using strings.TrimSpace()")
   fmt.Printf("%s \n", string1)
   fmt.Printf("%s \n", string2)
   fmt.Printf("%s \n", string3)
 
   response1 := strings.TrimSpace(string1)
   response2 := strings.TrimSpace(string2)
   response3 := strings.TrimSpace(string3)
   response3 = strings.Trim(response3, "$#")
   response3 = strings.TrimSpace(response3)
 
   fmt.Println("String after using strings.TrimSpace()")
   fmt.Printf("%s \n", response1)
   fmt.Printf("%s \n", response2)
   fmt.Printf("%s \n", response3)
}

Explanation

In the preceding example, strings.TrimSpace() method is supplied with different strings with different properties. string1 is a string with a leading white space, string2 is a string with trailing white space and string3 has leading and trailing white spaces. string3 has also leading and trailing unicode points. White spaces get removed from string1, string2 and string3.

After removing the white spaces in string3, it remains with leading and trailing unicode points. We then trim string3 to remove the leading and trailing unicode points , which leave white spaces. We finally remove the white spaces again using the strings.TrimSpace() method.

Output

$ go run main.go
String before using strings.TrimSpace()
 Hello world
Welcome to the GolinuxCloud Website 
$$ Hello world ## 
String after using strings.TrimSpace()
Hello world
Welcome to the GolinuxCloud Website
Hello world

 

string.TrimSuffix()

The strings.TrimSuffix() method is used to remove a suffix from a string. If the suffix does not exist, an unchanged string is returned.

Syntax

response := strings.TrimSuffix(s string)

The strings.TrimSuffix() method takes a string as a parameter. Returns a string without the suffix.

Example

package main
 
import (
   "fmt"
   "strings"
)
 
func main() {
   string1 := "Hello world Gophers!!!"
   fmt.Println("String before using strings.TrimSuffix()")
   fmt.Printf("%s \n", string1)
   response1 := strings.TrimSuffix(string1, " Gophers!!!")
 
   fmt.Println("String after using strings.TrimSuffix()")
   fmt.Printf("%s \n", response1)
}

Output

$ go run main.go
String before using strings.TrimSuffix()
Hello world Gophers!!!
String after using strings.TrimSuffix()
Hello world

 

strings.TrimPrefix()

The strings.TrimPrefix() method is used to remove a prefix from a string. If the prefix does not exist, an unchanged string is returned.

Syntax

response := strings.TrimPrefix(s string)

The strings.TrimPrefix() method takes a string as a parameter. Returns a string without the prefix.

Example

package main
 
import (
   "fmt"
   "strings"
)
 
func main() {
   string1 := "Gophers!!! Hello world"
   fmt.Println("String before using strings.TrimPrefix()")
   fmt.Printf("%s \n", string1)
   response1 := strings.TrimPrefix(string1, "Gophers!!! ")
 
   fmt.Println("String after using strings.TrimPrefix()")
   fmt.Printf("%s \n", response1)
}

Output

$ go run main.go
String before using strings.TrimPrefix()
Gophers!!! Hello world
String after using strings.TrimPrefix()
Hello world

 

Summary

While processing data, most of the time we are required to clean it. Go provides the string package that has functions to remove leading and trailing unicode points and white spaces. In this article we learn about the ways of trimming strings.

 

References

https://pkg.go.dev/strings

 

Views: 27

Antony Shikubu

He is highly skilled software developer with expertise in Python, Golang, and AWS cloud services. Skilled in building scalable solutions, he specializes in Django, Flask, Pandas, and NumPy for web apps and data processing, ensuring robust and maintainable code for diverse projects. You can connect with him on Linkedin.

Categories GO

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment