diff options
| author | fangguizhen <1297394526@qq.com> | 2023-01-20 09:43:40 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-01-20 23:21:39 +0000 |
| commit | 85b49d7f21dfbee9946bece01a168de239094716 (patch) | |
| tree | 3f345dc2fd0b7eb0eff7f65b7eef0eb6c9054e34 /src/strings/example_test.go | |
| parent | 0518e33f6c0c3a9f6ce1f800ca4b7fe5f3a1b7a5 (diff) | |
| download | go-85b49d7f21dfbee9946bece01a168de239094716.tar.xz | |
bytes,strings: add some examples
Change-Id: Ic93ad59119f3549c0f13c4f366f71e9d01b88c47
GitHub-Last-Rev: afb518047288976f440d3fe0d65923c1905a9b26
GitHub-Pull-Request: golang/go#57907
Reviewed-on: https://go-review.googlesource.com/c/go/+/462283
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/strings/example_test.go')
| -rw-r--r-- | src/strings/example_test.go | 45 |
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 +} |
