CutPrefix()
jameskeezer23 total contributions
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
.
Syntax
strings.CutPrefix(str, prefix)
Where str
is the given string and prefix
is the prefix to remove.
Example
The following example demonstrates the use of the strings.CutPrefix()
function.
package mainimport ("fmt""strings")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 trueCodecademy falseCodecademy true
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.