Commit a3537751 authored by Lyle Franklin's avatar Lyle Franklin Committed by Ian Lance Taylor

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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent d3c0af7a
...@@ -229,19 +229,6 @@ func ExampleToTitle() { ...@@ -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() { func ExampleMap() {
rot13 := func(r rune) rune { rot13 := func(r rune) rune {
switch { switch {
...@@ -256,11 +243,6 @@ func ExampleMap() { ...@@ -256,11 +243,6 @@ func ExampleMap() {
// Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... // 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() { func ExampleNewReplacer() {
r := strings.NewReplacer("<", "&lt;", ">", "&gt;") r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
fmt.Println(r.Replace("This is <b>HTML</b>!")) fmt.Println(r.Replace("This is <b>HTML</b>!"))
...@@ -277,18 +259,59 @@ func ExampleToLower() { ...@@ -277,18 +259,59 @@ func ExampleToLower() {
// Output: gopher // Output: gopher
} }
func ExampleTrimSuffix() { func ExampleTrim() {
var s = "Hello, goodbye, etc!" fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
s = strings.TrimSuffix(s, "goodbye, etc!") // Output: Hello, Gophers
s = strings.TrimSuffix(s, "planet") }
fmt.Print(s, "world!")
// Output: Hello, world! func ExampleTrimSpace() {
fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
// Output: Hello, Gophers
} }
func ExampleTrimPrefix() { func ExampleTrimPrefix() {
var s = "Goodbye,, world!" var s = "¡¡¡Hello, Gophers!!!"
s = strings.TrimPrefix(s, "Goodbye,") s = strings.TrimPrefix(s, "¡¡¡Hello, ")
s = strings.TrimPrefix(s, "Howdy,") s = strings.TrimPrefix(s, "¡¡¡Howdy, ")
fmt.Print("Hello" + s) fmt.Print(s)
// Output: Hello, world! // 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
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment