Cut()

4hbab's avatar
Published Jul 4, 2023Updated Jul 5, 2023
Contribute to Docs

The Cut() function slices a string around a separator. The strings library must be imported in order to use this function.

Syntax

before, after, found := strings.Cut(s, sep)

Where before is the part of the string before the separator, after is the part of the string after the separator, and found is a boolean indicating whether the separator was found in the string.

Example

The following example removes the substring world from the original string Hello, world! and prints out the result:

package main
import (
"fmt"
"strings"
)
func main() {
s := "Hello, world!"
sep := ","
before, after, found := strings.Cut(s, sep)
fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
}

The output will be:

Cut("Hello, world!", ",") = "Hello", " world!", true

All contributors

Contribute to Docs

Learn Go on Codecademy