]> Cypherpunks repositories - gostls13.git/commitdiff
strings: add Examples for TrimFunc and variants during Gophercon!
authorLyle Franklin <lylejfranklin@gmail.com>
Sat, 15 Jul 2017 20:33:56 +0000 (13:33 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 8 Aug 2017 14:26:13 +0000 (14:26 +0000)
Change-Id: I6bfe5b914cf11be1cd1f8e61d557cc718725f0be
Reviewed-on: https://go-review.googlesource.com/49013
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/strings/example_test.go

index e9621522ef223de9e05cc229295768f244c4d586..f35452369d44707e584cd64247745aab4a1eb4b2 100644 (file)
@@ -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("<", "&lt;", ">", "&gt;")
        fmt.Println(r.Replace("This is <b>HTML</b>!"))
@@ -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
 }