Cut()
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 mainimport ("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
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.