1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 func ExampleFields() {
13 fmt
.Printf("Fields are: %q", strings
.Fields(" foo bar baz "))
14 // Output: Fields are: ["foo" "bar" "baz"]
17 func ExampleContains() {
18 fmt
.Println(strings
.Contains("seafood", "foo"))
19 fmt
.Println(strings
.Contains("seafood", "bar"))
20 fmt
.Println(strings
.Contains("seafood", ""))
21 fmt
.Println(strings
.Contains("", ""))
29 func ExampleContainsAny() {
30 fmt
.Println(strings
.ContainsAny("team", "i"))
31 fmt
.Println(strings
.ContainsAny("failure", "u & i"))
32 fmt
.Println(strings
.ContainsAny("foo", ""))
33 fmt
.Println(strings
.ContainsAny("", ""))
42 fmt
.Println(strings
.Count("cheese", "e"))
43 fmt
.Println(strings
.Count("five", "")) // before & after each rune
49 func ExampleEqualFold() {
50 fmt
.Println(strings
.EqualFold("Go", "go"))
55 fmt
.Println(strings
.Index("chicken", "ken"))
56 fmt
.Println(strings
.Index("chicken", "dmr"))
62 func ExampleIndexRune() {
63 fmt
.Println(strings
.IndexRune("chicken", 'k'))
64 fmt
.Println(strings
.IndexRune("chicken", 'd'))
70 func ExampleLastIndex() {
71 fmt
.Println(strings
.Index("go gopher", "go"))
72 fmt
.Println(strings
.LastIndex("go gopher", "go"))
73 fmt
.Println(strings
.LastIndex("go gopher", "rodent"))
81 s
:= []string{"foo", "bar", "baz"}
82 fmt
.Println(strings
.Join(s
, ", "))
83 // Output: foo, bar, baz
86 func ExampleRepeat() {
87 fmt
.Println("ba" + strings
.Repeat("na", 2))
91 func ExampleReplace() {
92 fmt
.Println(strings
.Replace("oink oink oink", "k", "ky", 2))
93 fmt
.Println(strings
.Replace("oink oink oink", "oink", "moo", -1))
100 fmt
.Printf("%q\n", strings
.Split("a,b,c", ","))
101 fmt
.Printf("%q\n", strings
.Split("a man a plan a canal panama", "a "))
102 fmt
.Printf("%q\n", strings
.Split(" xyz ", ""))
103 fmt
.Printf("%q\n", strings
.Split("", "Bernardo O'Higgins"))
106 // ["" "man " "plan " "canal panama"]
107 // [" " "x" "y" "z" " "]
111 func ExampleSplitN() {
112 fmt
.Printf("%q\n", strings
.SplitN("a,b,c", ",", 2))
113 z
:= strings
.SplitN("a,b,c", ",", 0)
114 fmt
.Printf("%q (nil = %v)\n", z
, z
== nil)
120 func ExampleSplitAfter() {
121 fmt
.Printf("%q\n", strings
.SplitAfter("a,b,c", ","))
122 // Output: ["a," "b," "c"]
125 func ExampleSplitAfterN() {
126 fmt
.Printf("%q\n", strings
.SplitAfterN("a,b,c", ",", 2))
127 // Output: ["a," "b,c"]
130 func ExampleTitle() {
131 fmt
.Println(strings
.Title("her royal highness"))
132 // Output: Her Royal Highness
135 func ExampleToTitle() {
136 fmt
.Println(strings
.ToTitle("loud noises"))
137 fmt
.Println(strings
.ToTitle("хлеб"))
144 fmt
.Printf("[%q]", strings
.Trim(" !!! Achtung !!! ", "! "))
145 // Output: ["Achtung"]
149 rot13
:= func(r rune
) rune
{
151 case r
>= 'A' && r
<= 'Z':
152 return 'A' + (r
-'A'+13)%26
153 case r
>= 'a' && r
<= 'z':
154 return 'a' + (r
-'a'+13)%26
158 fmt
.Println(strings
.Map(rot13
, "'Twas brillig and the slithy gopher..."))
159 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
162 func ExampleTrimSpace() {
163 fmt
.Println(strings
.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
164 // Output: a lone gopher
167 func ExampleNewReplacer() {
168 r
:= strings
.NewReplacer("<", "<", ">", ">")
169 fmt
.Println(r
.Replace("This is <b>HTML</b>!"))
170 // Output: This is <b>HTML</b>!
173 func ExampleToUpper() {
174 fmt
.Println(strings
.ToUpper("Gopher"))
178 func ExampleToLower() {
179 fmt
.Println(strings
.ToLower("Gopher"))
183 func ExampleTrimSuffix() {
184 var s
= "Hello, goodbye, etc!"
185 s
= strings
.TrimSuffix(s
, "goodbye, etc!")
186 s
= strings
.TrimSuffix(s
, "planet")
187 fmt
.Print(s
, "world!")
188 // Output: Hello, world!
191 func ExampleTrimPrefix() {
192 var s
= "Goodbye,, world!"
193 s
= strings
.TrimPrefix(s
, "Goodbye,")
194 s
= strings
.TrimPrefix(s
, "Howdy,")
195 fmt
.Print("Hello" + s
)
196 // Output: Hello, world!