Published Aug 29, 2023
Contribute to Docs

The CutPrefix() function in Go is used to return a specified string without a given prefix. The function returns the specified string without the given prefix and boolean value that confirms the presence of the prefix. It returns true if the given string has the specified prefix, otherwise it returns the full string and false. If the given prefix is an empty string, the function returns the full string and true.


strings.CutPrefix(str, prefix)

Where str is the given string and prefix is the prefix to remove.


The following example demonstrates the use of the strings.CutPrefix() function.

package main
import (
func main() {
str := "Codecademy"
firstPrefix := "Code"
secondPrefix := "Hello"
thirdPrefix := ""
fmt.Println(strings.CutPrefix(str, firstPrefix))
fmt.Println(strings.CutPrefix(str, secondPrefix))
fmt.Println(strings.CutPrefix(str, thirdPrefix))

The output will be:

cademy true
Codecademy false
Codecademy true

All contributors

Looking to contribute?

Learn Go on Codecademy