CutSuffix()

jameskeezer11 total contributions
Published Sep 23, 2023
Contribute to Docs
The CutSuffix()
function in Go is used to return a specified string without a given suffix. The function returns the specified string without the given suffix and a boolean value confirming the presence of the suffix. It returns true
if the given string has the specified suffix, otherwise it returns the full string and false
. If the given suffix is an empty string, the function returns the full string and true
.
Syntax
strings.CutSuffix(str, suffix)
Where str
is the given string and suffix
is the suffix to remove.
Example
The following example demonstrates the use of the strings.CutSuffix()
function.
package mainimport ("fmt""strings")func main() {str := "Hello Codecademy"suffix1 := "ademy"suffix2 := "llo"suffix3 := ""fmt.Println(strings.CutSuffix(str, suffix1))fmt.Println(strings.CutSuffix(str, suffix2))fmt.Println(strings.CutSuffix(str, suffix3))}
The output will be:
Hello Code trueHello Codecademy falseHello Codecademy false
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.