CutSuffix()

Published Sep 23, 2023Updated Aug 23, 2024
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 main
import (
"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 Codec true
Hello Codecademy false
Hello Codecademy true

All contributors

Looking to contribute?

Learn Go on Codecademy