In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. You can trim a string in different ways using the following list of functions. All these functions are defined under strings package, so you have to import strings package in your program to access these functions.
1. Trim: This function is used to trim the string all the leading and trailing Unicode code points which are specified in this function.
Syntax:
func Trim(str string, cutstr string) string
Here, str represent the current string and cutstr represents the elements which you want to trim in the given string.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "!!Welcome to GeeksforGeeks !!"
str2 := "@@This is the tutorial of Golang$$"
fmt.Println( "Strings before trimming:" )
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2:" , str2)
res1 := strings.Trim(str1, "!" )
res2 := strings.Trim(str2, "@$" )
fmt.Println( "\nStrings after trimming:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2:" , res2)
}
|
Output:
Strings before trimming:
String 1: !!Welcome to GeeksforGeeks !!
String 2: @@This is the tutorial of Golang$$
Strings after trimming:
Result 1: Welcome to GeeksforGeeks
Result 2: This is the tutorial of Golang
2. TrimLeft: This function is used to trim the left-hand side(specified in the function) Unicode code points of the string.
Syntax:
func TrimLeft(str string, cutstr string) string
Here, str represent the current string and cutstr represents the left-hand side elements which you want to trim in the given string.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "!!Welcome to GeeksforGeeks **"
str2 := "@@This is the tutorial of Golang$$"
fmt.Println( "Strings before trimming:" )
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2:" , str2)
res1 := strings.TrimLeft(str1, "!*" )
res2 := strings.TrimLeft(str2, "@" )
fmt.Println( "\nStrings after trimming:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2:" , res2)
}
|
Output:
Strings before trimming:
String 1: !!Welcome to GeeksforGeeks **
String 2: @@This is the tutorial of Golang$$
Strings after trimming:
Result 1: Welcome to GeeksforGeeks **
Result 2: This is the tutorial of Golang$$
3. TrimRight: This function is used to trim the right-hand side(specified in the function) Unicode code points of the string.
Syntax:
func TrimRight(str string, cutstr string) string
Here, str represent the current string and cutstr represents the right-hand side elements which you want to trim in the given string.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "!!Welcome to GeeksforGeeks **"
str2 := "@@This is the tutorial of Golang$$"
fmt.Println( "Strings before trimming:" )
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2:" , str2)
res1 := strings.TrimRight(str1, "!*" )
res2 := strings.TrimRight(str2, "$" )
fmt.Println( "\nStrings after trimming:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2:" , res2)
}
|
Output:
Strings before trimming:
String 1: !!Welcome to GeeksforGeeks **
String 2: @@This is the tutorial of Golang$$
Strings after trimming:
Result 1: !!Welcome to GeeksforGeeks
Result 2: @@This is the tutorial of Golang
4. TrimSpace: This function is used to trim all the leading and trailing white space from the specified string.
Syntax:
func TrimSpace(str string) string
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := " **Welcome to GeeksforGeeks** "
str2 := " ##This is the tutorial of Golang## "
fmt.Println( "Strings before trimming:" )
fmt.Println(str1, str2)
res1 := strings.TrimSpace(str1)
res2 := strings.TrimSpace(str2)
fmt.Println( "\nStrings after trimming:" )
fmt.Println(res1, res2)
}
|
Output:
Strings before trimming:
**Welcome to GeeksforGeeks** ##This is the tutorial of Golang##
Strings after trimming:
**Welcome to GeeksforGeeks** ##This is the tutorial of Golang##
5. TrimSuffix: This method is used to trim the trailing suffix string from the given string. If the given string does not contain the specified suffix string, then this function returns the original string without any change.
Syntax:
func TrimSuffix(str, suffstr string) string
Here, str represents the original string and suffstr represent the suffix string.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "Welcome, GeeksforGeeks"
str2 := "This is the, tutorial of Golang"
fmt.Println( "Strings before trimming:" )
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2:" , str2)
res1 := strings.TrimSuffix(str1, "GeeksforGeeks" )
res2 := strings.TrimSuffix(str2, "Hello" )
fmt.Println( "\nStrings after trimming:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2:" , res2)
}
|
Output:
Strings before trimming:
String 1: Welcome, GeeksforGeeks
String 2: This is the, tutorial of Golang
Strings after trimming:
Result 1: Welcome,
Result 2: This is the, tutorial of Golang
6. TrimPrefix: This method is used to trim the leading prefix string from the given string. If the given string does not contain the specified prefix string, then this function returns the original string without any change.
Syntax:
func TrimPrefix(str, suffstr string) string
Here, str represents the original string and suffstr represent the prefix string.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "Welcome, GeeksforGeeks"
str2 := "This is the, tutorial of Golang"
fmt.Println( "Strings before trimming:" )
fmt.Println( "String 1: " , str1)
fmt.Println( "String 2: " , str2)
res1 := strings.TrimPrefix(str1, "Welcome" )
res2 := strings.TrimPrefix(str2, "Hello" )
fmt.Println( "\nStrings after trimming:" )
fmt.Println( "Result 1: " , res1)
fmt.Println( "Result 2: " , res2)
}
|
Output:
Strings before trimming:
String 1: Welcome, GeeksforGeeks
String 2: This is the, tutorial of Golang
Strings after trimming:
Result 1: , GeeksforGeeks
Result 2: This is the, tutorial of Golang