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 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 Codec trueHello Codecademy falseHello Codecademy true
Contribute to Docs
- 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.
Learn Go on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours