From: Lyle Franklin Date: Sat, 15 Jul 2017 20:33:56 +0000 (-0700) Subject: strings: add Examples for TrimFunc and variants during Gophercon! X-Git-Tag: go1.10beta1~1700 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a35377515f6a232305a52f89c164d2782951bc11;p=gostls13.git strings: add Examples for TrimFunc and variants during Gophercon! Change-Id: I6bfe5b914cf11be1cd1f8e61d557cc718725f0be Reviewed-on: https://go-review.googlesource.com/49013 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/strings/example_test.go b/src/strings/example_test.go index e9621522ef..f35452369d 100644 --- a/src/strings/example_test.go +++ b/src/strings/example_test.go @@ -229,19 +229,6 @@ func ExampleToTitle() { // ХЛЕБ } -func ExampleTrim() { - fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! ")) - // Output: ["Achtung! Achtung"] -} - -func ExampleTrimFunc() { - f := func(c rune) bool { - return !unicode.IsLetter(c) && !unicode.IsNumber(c) - } - fmt.Printf("[%q]", strings.TrimFunc(" Achtung1! Achtung2,...", f)) - // Output: ["Achtung1! Achtung2"] -} - func ExampleMap() { rot13 := func(r rune) rune { switch { @@ -256,11 +243,6 @@ func ExampleMap() { // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... } -func ExampleTrimSpace() { - fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) - // Output: a lone gopher -} - func ExampleNewReplacer() { r := strings.NewReplacer("<", "<", ">", ">") fmt.Println(r.Replace("This is HTML!")) @@ -277,18 +259,59 @@ func ExampleToLower() { // Output: gopher } -func ExampleTrimSuffix() { - var s = "Hello, goodbye, etc!" - s = strings.TrimSuffix(s, "goodbye, etc!") - s = strings.TrimSuffix(s, "planet") - fmt.Print(s, "world!") - // Output: Hello, world! +func ExampleTrim() { + fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) + // Output: Hello, Gophers +} + +func ExampleTrimSpace() { + fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) + // Output: Hello, Gophers } func ExampleTrimPrefix() { - var s = "Goodbye,, world!" - s = strings.TrimPrefix(s, "Goodbye,") - s = strings.TrimPrefix(s, "Howdy,") - fmt.Print("Hello" + s) - // Output: Hello, world! + var s = "¡¡¡Hello, Gophers!!!" + s = strings.TrimPrefix(s, "¡¡¡Hello, ") + s = strings.TrimPrefix(s, "¡¡¡Howdy, ") + fmt.Print(s) + // Output: Gophers!!! +} + +func ExampleTrimSuffix() { + var s = "¡¡¡Hello, Gophers!!!" + s = strings.TrimSuffix(s, ", Gophers!!!") + s = strings.TrimSuffix(s, ", Marmots!!!") + fmt.Print(s) + // Output: ¡¡¡Hello +} + +func ExampleTrimFunc() { + fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { + return !unicode.IsLetter(r) && !unicode.IsNumber(r) + })) + // Output: Hello, Gophers +} + +func ExampleTrimLeft() { + fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) + // Output: Hello, Gophers!!! +} + +func ExampleTrimLeftFunc() { + fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { + return !unicode.IsLetter(r) && !unicode.IsNumber(r) + })) + // Output: Hello, Gophers!!! +} + +func ExampleTrimRight() { + fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡")) + // Output: ¡¡¡Hello, Gophers +} + +func ExampleTrimRightFunc() { + fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { + return !unicode.IsLetter(r) && !unicode.IsNumber(r) + })) + // Output: ¡¡¡Hello, Gophers }