Home »
Golang »
Golang Reference
Golang strings.TrimSuffix() Function with Examples
Golang | strings.TrimSuffix() Function: Here, we are going to learn about the TrimSuffix() function of strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 23, 2021
strings.TrimSuffix()
The TrimSuffix() function is an inbuilt function of strings package which is used to get a string without the provided leading suffix string. It accepts two parameters – a string and a suffix string and returns the string after trimming the suffix.
If the string doesn't end with the given suffix, the string is returned unchanged.
Syntax
func TrimSuffix(str, suffix string) string
Parameters
- str : The string in which we have to remove suffix string
- suffix : The suffix string to be trimmed.
Return Value
The return type of TrimSuffix() function is a string, it returns a string without the provided leading suffix string.
Example 1
// Golang program to demonstrate the
// example of strings.TrimSuffix() Function
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.TrimSuffix(" Hello, world.", ", world."))
fmt.Println(strings.TrimSuffix("...Hello, world...", "..."))
fmt.Println(strings.TrimSuffix("###Hello, world!!!", ", world!!!"))
fmt.Println(strings.TrimSuffix("Hi! World!", "!"))
fmt.Println(strings.TrimSuffix("###Hello, world!!!", "world."))
}
Output:
Hello
...Hello, world
###Hello
Hi! World
###Hello, world!!!
Example 2
// Golang program to demonstrate the
// example of strings.TrimSuffix() Function
package main
import (
"fmt"
"strings"
)
func main() {
var str string
var result string
str = "***Hi, there! How are you?"
result = strings.TrimSuffix(str, "you?")
fmt.Println("Actual string :", str)
fmt.Println("Trimmed string:", result)
str = "Hi, how are you?"
result = strings.TrimSuffix(str, "?")
fmt.Println("Actual string :", str)
fmt.Println("Trimmed string:", result)
str = "¡¡¡Hello, world!!!"
result = strings.TrimSuffix(str, "!!!")
fmt.Println("Actual string :", str)
fmt.Println("Trimmed string:", result)
}
Output:
Actual string : ***Hi, there! How are you?
Trimmed string: ***Hi, there! How are
Actual string : Hi, how are you?
Trimmed string: Hi, how are you
Actual string : ¡¡¡Hello, world!!!
Trimmed string: ¡¡¡Hello, world
Golang strings Package Functions »