aboutsummaryrefslogtreecommitdiff
path: root/src/strings
diff options
context:
space:
mode:
Diffstat (limited to 'src/strings')
-rw-r--r--src/strings/example_test.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/strings/example_test.go b/src/strings/example_test.go
index 2a59512ceb..ab83e10de4 100644
--- a/src/strings/example_test.go
+++ b/src/strings/example_test.go
@@ -8,8 +8,19 @@ import (
"fmt"
"strings"
"unicode"
+ "unsafe"
)
+func ExampleClone() {
+ s := "abc"
+ clone := strings.Clone(s)
+ fmt.Println(s == clone)
+ fmt.Println(unsafe.StringData(s) == unsafe.StringData(clone))
+ // Output:
+ // true
+ // false
+}
+
func ExampleBuilder() {
var b strings.Builder
for i := 3; i >= 1; i-- {
@@ -93,6 +104,30 @@ func ExampleCut() {
// Cut("Gopher", "Badger") = "Gopher", "", false
}
+func ExampleCutPrefix() {
+ show := func(s, sep string) {
+ after, found := strings.CutPrefix(s, sep)
+ fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
+ }
+ show("Gopher", "Go")
+ show("Gopher", "ph")
+ // Output:
+ // CutPrefix("Gopher", "Go") = "pher", true
+ // CutPrefix("Gopher", "ph") = "Gopher", false
+}
+
+func ExampleCutSuffix() {
+ show := func(s, sep string) {
+ before, found := strings.CutSuffix(s, sep)
+ fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found)
+ }
+ show("Gopher", "Go")
+ show("Gopher", "er")
+ // Output:
+ // CutSuffix("Gopher", "Go") = "Gopher", false
+ // CutSuffix("Gopher", "er") = "Goph", true
+}
+
func ExampleEqualFold() {
fmt.Println(strings.EqualFold("Go", "go"))
fmt.Println(strings.EqualFold("AB", "ab")) // true because comparison uses simple case-folding
@@ -402,3 +437,13 @@ func ExampleTrimRightFunc() {
}))
// Output: ¡¡¡Hello, Gophers
}
+
+func ExampleToValidUTF8() {
+ fmt.Printf("%s\n", strings.ToValidUTF8("abc", "\uFFFD"))
+ fmt.Printf("%s\n", strings.ToValidUTF8("a\xffb\xC0\xAFc\xff", ""))
+ fmt.Printf("%s\n", strings.ToValidUTF8("\xed\xa0\x80", "abc"))
+ // Output:
+ // abc
+ // abc
+ // abc
+}