How to Trim a String in Go Language? | Go Programming Tutorial
How to Trim a String in Go Language? | Go Programming Tutorial
In this article, you will learn how to trim a String in the Go Programming Language.
In Go Programming Language, Strings are different from other programming languages. In GoLang, it is a sequence of variable-width characters where every character is indicative by one or more bytes using UTF-8 encoding. In simple words, strings are the immutable chain of arbitrary bytes which also includes bytes with zero values.
There are multiple functions available through which the user can trim the string. All these functions are defined under the string package, so the user has to import the string package in your program to access the trim functions.
- Trim: Trim function is used to trim the string of all the leading and trailing Unicode points which are mentioned in the function.
Syntax:
func Trim(str string, cutstr string) string
Here, str represents the current string and cutstr represents the elements that you want to trim in the mentioned string.
- TrimLeft: TrimLeft function is used to trim the left-hand side of the string.
Syntax:
func TrimLeft(str string, cutstr string) string
Here, str represents the given string and cutstr represents the elements that need to be cut from the left-hand side of the string.
- TrimRight: TrimRight function is used to trim the right-hand side of the string.
Syntax:
func TrimRight(str string, cutstr string) string
Here, str represents the given string and cutstr represents the right-hand side of the string.
- TrimSpace: TrimSpace is used to remove all the leading and trailing white space from the specified string.
Syntax:
func TrimSpace(str string) string
- TrimSuffix: TrimSuffix is used to remove the trailing suffix string from the current string. If the current string does not contain the particular suffix string, then it will return the original string without any alteration.
Syntax:
func TrimSuffix(str, suffstr string) string
Here, str represents the given string and suffstr represents the suffix string.
- TrimPrefix: TrimPrefix is used to remove the leading prefix string from the current string. If the given string does not have a particular prefix string, then it will return the original string without any alteration.
Syntax:
func TrimPrefix(str, suffstr string) string
Here, str represents the given string and suffstr represents the prefix string.