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.
13 func ExampleFields() {
14 fmt
.Printf("Fields are: %q", strings
.Fields(" foo bar baz "))
15 // Output: Fields are: ["foo" "bar" "baz"]
18 func ExampleFieldsFunc() {
19 f
:= func(c rune
) bool {
20 return !unicode
.IsLetter(c
) && !unicode
.IsNumber(c
)
22 fmt
.Printf("Fields are: %q", strings
.FieldsFunc(" foo1;bar2,baz3...", f
))
23 // Output: Fields are: ["foo1" "bar2" "baz3"]
26 func ExampleContains() {
27 fmt
.Println(strings
.Contains("seafood", "foo"))
28 fmt
.Println(strings
.Contains("seafood", "bar"))
29 fmt
.Println(strings
.Contains("seafood", ""))
30 fmt
.Println(strings
.Contains("", ""))
38 func ExampleContainsAny() {
39 fmt
.Println(strings
.ContainsAny("team", "i"))
40 fmt
.Println(strings
.ContainsAny("failure", "u & i"))
41 fmt
.Println(strings
.ContainsAny("foo", ""))
42 fmt
.Println(strings
.ContainsAny("", ""))
51 fmt
.Println(strings
.Count("cheese", "e"))
52 fmt
.Println(strings
.Count("five", "")) // before & after each rune
58 func ExampleEqualFold() {
59 fmt
.Println(strings
.EqualFold("Go", "go"))
64 fmt
.Println(strings
.Index("chicken", "ken"))
65 fmt
.Println(strings
.Index("chicken", "dmr"))
71 func ExampleIndexFunc() {
72 f
:= func(c rune
) bool {
73 return unicode
.Is(unicode
.Han
, c
)
75 fmt
.Println(strings
.IndexFunc("Hello, 世界", f
))
76 fmt
.Println(strings
.IndexFunc("Hello, world", f
))
82 func ExampleIndexAny() {
83 fmt
.Println(strings
.IndexAny("chicken", "aeiouy"))
84 fmt
.Println(strings
.IndexAny("crwth", "aeiouy"))
90 func ExampleIndexRune() {
91 fmt
.Println(strings
.IndexRune("chicken", 'k'))
92 fmt
.Println(strings
.IndexRune("chicken", 'd'))
98 func ExampleLastIndex() {
99 fmt
.Println(strings
.Index("go gopher", "go"))
100 fmt
.Println(strings
.LastIndex("go gopher", "go"))
101 fmt
.Println(strings
.LastIndex("go gopher", "rodent"))
109 s
:= []string{"foo", "bar", "baz"}
110 fmt
.Println(strings
.Join(s
, ", "))
111 // Output: foo, bar, baz
114 func ExampleRepeat() {
115 fmt
.Println("ba" + strings
.Repeat("na", 2))
119 func ExampleReplace() {
120 fmt
.Println(strings
.Replace("oink oink oink", "k", "ky", 2))
121 fmt
.Println(strings
.Replace("oink oink oink", "oink", "moo", -1))
127 func ExampleSplit() {
128 fmt
.Printf("%q\n", strings
.Split("a,b,c", ","))
129 fmt
.Printf("%q\n", strings
.Split("a man a plan a canal panama", "a "))
130 fmt
.Printf("%q\n", strings
.Split(" xyz ", ""))
131 fmt
.Printf("%q\n", strings
.Split("", "Bernardo O'Higgins"))
134 // ["" "man " "plan " "canal panama"]
135 // [" " "x" "y" "z" " "]
139 func ExampleSplitN() {
140 fmt
.Printf("%q\n", strings
.SplitN("a,b,c", ",", 2))
141 z
:= strings
.SplitN("a,b,c", ",", 0)
142 fmt
.Printf("%q (nil = %v)\n", z
, z
== nil)
148 func ExampleSplitAfter() {
149 fmt
.Printf("%q\n", strings
.SplitAfter("a,b,c", ","))
150 // Output: ["a," "b," "c"]
153 func ExampleSplitAfterN() {
154 fmt
.Printf("%q\n", strings
.SplitAfterN("a,b,c", ",", 2))
155 // Output: ["a," "b,c"]
158 func ExampleTitle() {
159 fmt
.Println(strings
.Title("her royal highness"))
160 // Output: Her Royal Highness
163 func ExampleToTitle() {
164 fmt
.Println(strings
.ToTitle("loud noises"))
165 fmt
.Println(strings
.ToTitle("хлеб"))
172 fmt
.Printf("[%q]", strings
.Trim(" !!! Achtung! Achtung! !!! ", "! "))
173 // Output: ["Achtung! Achtung"]
177 rot13
:= func(r rune
) rune
{
179 case r
>= 'A' && r
<= 'Z':
180 return 'A' + (r
-'A'+13)%26
181 case r
>= 'a' && r
<= 'z':
182 return 'a' + (r
-'a'+13)%26
186 fmt
.Println(strings
.Map(rot13
, "'Twas brillig and the slithy gopher..."))
187 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
190 func ExampleTrimSpace() {
191 fmt
.Println(strings
.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
192 // Output: a lone gopher
195 func ExampleNewReplacer() {
196 r
:= strings
.NewReplacer("<", "<", ">", ">")
197 fmt
.Println(r
.Replace("This is <b>HTML</b>!"))
198 // Output: This is <b>HTML</b>!
201 func ExampleToUpper() {
202 fmt
.Println(strings
.ToUpper("Gopher"))
206 func ExampleToLower() {
207 fmt
.Println(strings
.ToLower("Gopher"))
211 func ExampleTrimSuffix() {
212 var s
= "Hello, goodbye, etc!"
213 s
= strings
.TrimSuffix(s
, "goodbye, etc!")
214 s
= strings
.TrimSuffix(s
, "planet")
215 fmt
.Print(s
, "world!")
216 // Output: Hello, world!
219 func ExampleTrimPrefix() {
220 var s
= "Goodbye,, world!"
221 s
= strings
.TrimPrefix(s
, "Goodbye,")
222 s
= strings
.TrimPrefix(s
, "Howdy,")
223 fmt
.Print("Hello" + s
)
224 // Output: Hello, world!