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 ExampleBuilder() {
15 for i
:= 3; i
>= 1; i
-- {
16 fmt
.Fprintf(&b
, "%d...", i
)
18 b
.WriteString("ignition")
19 fmt
.Println(b
.String())
21 // Output: 3...2...1...ignition
24 func ExampleCompare() {
25 fmt
.Println(strings
.Compare("a", "b"))
26 fmt
.Println(strings
.Compare("a", "a"))
27 fmt
.Println(strings
.Compare("b", "a"))
34 func ExampleContains() {
35 fmt
.Println(strings
.Contains("seafood", "foo"))
36 fmt
.Println(strings
.Contains("seafood", "bar"))
37 fmt
.Println(strings
.Contains("seafood", ""))
38 fmt
.Println(strings
.Contains("", ""))
46 func ExampleContainsAny() {
47 fmt
.Println(strings
.ContainsAny("team", "i"))
48 fmt
.Println(strings
.ContainsAny("fail", "ui"))
49 fmt
.Println(strings
.ContainsAny("ure", "ui"))
50 fmt
.Println(strings
.ContainsAny("failure", "ui"))
51 fmt
.Println(strings
.ContainsAny("foo", ""))
52 fmt
.Println(strings
.ContainsAny("", ""))
62 func ExampleContainsRune() {
63 // Finds whether a string contains a particular Unicode code point.
64 // The code point for the lowercase letter "a", for example, is 97.
65 fmt
.Println(strings
.ContainsRune("aardvark", 97))
66 fmt
.Println(strings
.ContainsRune("timeout", 97))
73 fmt
.Println(strings
.Count("cheese", "e"))
74 fmt
.Println(strings
.Count("five", "")) // before & after each rune
81 show
:= func(s
, sep
string) {
82 before
, after
, found
:= strings
.Cut(s
, sep
)
83 fmt
.Printf("Cut(%q, %q) = %q, %q, %v\n", s
, sep
, before
, after
, found
)
88 show("Gopher", "Badger")
90 // Cut("Gopher", "Go") = "", "pher", true
91 // Cut("Gopher", "ph") = "Go", "er", true
92 // Cut("Gopher", "er") = "Goph", "", true
93 // Cut("Gopher", "Badger") = "Gopher", "", false
96 func ExampleEqualFold() {
97 fmt
.Println(strings
.EqualFold("Go", "go"))
101 func ExampleFields() {
102 fmt
.Printf("Fields are: %q", strings
.Fields(" foo bar baz "))
103 // Output: Fields are: ["foo" "bar" "baz"]
106 func ExampleFieldsFunc() {
107 f
:= func(c rune
) bool {
108 return !unicode
.IsLetter(c
) && !unicode
.IsNumber(c
)
110 fmt
.Printf("Fields are: %q", strings
.FieldsFunc(" foo1;bar2,baz3...", f
))
111 // Output: Fields are: ["foo1" "bar2" "baz3"]
114 func ExampleHasPrefix() {
115 fmt
.Println(strings
.HasPrefix("Gopher", "Go"))
116 fmt
.Println(strings
.HasPrefix("Gopher", "C"))
117 fmt
.Println(strings
.HasPrefix("Gopher", ""))
124 func ExampleHasSuffix() {
125 fmt
.Println(strings
.HasSuffix("Amigo", "go"))
126 fmt
.Println(strings
.HasSuffix("Amigo", "O"))
127 fmt
.Println(strings
.HasSuffix("Amigo", "Ami"))
128 fmt
.Println(strings
.HasSuffix("Amigo", ""))
136 func ExampleIndex() {
137 fmt
.Println(strings
.Index("chicken", "ken"))
138 fmt
.Println(strings
.Index("chicken", "dmr"))
144 func ExampleIndexFunc() {
145 f
:= func(c rune
) bool {
146 return unicode
.Is(unicode
.Han
, c
)
148 fmt
.Println(strings
.IndexFunc("Hello, 世界", f
))
149 fmt
.Println(strings
.IndexFunc("Hello, world", f
))
155 func ExampleIndexAny() {
156 fmt
.Println(strings
.IndexAny("chicken", "aeiouy"))
157 fmt
.Println(strings
.IndexAny("crwth", "aeiouy"))
163 func ExampleIndexByte() {
164 fmt
.Println(strings
.IndexByte("golang", 'g'))
165 fmt
.Println(strings
.IndexByte("gophers", 'h'))
166 fmt
.Println(strings
.IndexByte("golang", 'x'))
172 func ExampleIndexRune() {
173 fmt
.Println(strings
.IndexRune("chicken", 'k'))
174 fmt
.Println(strings
.IndexRune("chicken", 'd'))
180 func ExampleLastIndex() {
181 fmt
.Println(strings
.Index("go gopher", "go"))
182 fmt
.Println(strings
.LastIndex("go gopher", "go"))
183 fmt
.Println(strings
.LastIndex("go gopher", "rodent"))
190 func ExampleLastIndexAny() {
191 fmt
.Println(strings
.LastIndexAny("go gopher", "go"))
192 fmt
.Println(strings
.LastIndexAny("go gopher", "rodent"))
193 fmt
.Println(strings
.LastIndexAny("go gopher", "fail"))
200 func ExampleLastIndexByte() {
201 fmt
.Println(strings
.LastIndexByte("Hello, world", 'l'))
202 fmt
.Println(strings
.LastIndexByte("Hello, world", 'o'))
203 fmt
.Println(strings
.LastIndexByte("Hello, world", 'x'))
210 func ExampleLastIndexFunc() {
211 fmt
.Println(strings
.LastIndexFunc("go 123", unicode
.IsNumber
))
212 fmt
.Println(strings
.LastIndexFunc("123 go", unicode
.IsNumber
))
213 fmt
.Println(strings
.LastIndexFunc("go", unicode
.IsNumber
))
221 s
:= []string{"foo", "bar", "baz"}
222 fmt
.Println(strings
.Join(s
, ", "))
223 // Output: foo, bar, baz
226 func ExampleRepeat() {
227 fmt
.Println("ba" + strings
.Repeat("na", 2))
231 func ExampleReplace() {
232 fmt
.Println(strings
.Replace("oink oink oink", "k", "ky", 2))
233 fmt
.Println(strings
.Replace("oink oink oink", "oink", "moo", -1))
239 func ExampleReplaceAll() {
240 fmt
.Println(strings
.ReplaceAll("oink oink oink", "oink", "moo"))
245 func ExampleSplit() {
246 fmt
.Printf("%q\n", strings
.Split("a,b,c", ","))
247 fmt
.Printf("%q\n", strings
.Split("a man a plan a canal panama", "a "))
248 fmt
.Printf("%q\n", strings
.Split(" xyz ", ""))
249 fmt
.Printf("%q\n", strings
.Split("", "Bernardo O'Higgins"))
252 // ["" "man " "plan " "canal panama"]
253 // [" " "x" "y" "z" " "]
257 func ExampleSplitN() {
258 fmt
.Printf("%q\n", strings
.SplitN("a,b,c", ",", 2))
259 z
:= strings
.SplitN("a,b,c", ",", 0)
260 fmt
.Printf("%q (nil = %v)\n", z
, z
== nil)
266 func ExampleSplitAfter() {
267 fmt
.Printf("%q\n", strings
.SplitAfter("a,b,c", ","))
268 // Output: ["a," "b," "c"]
271 func ExampleSplitAfterN() {
272 fmt
.Printf("%q\n", strings
.SplitAfterN("a,b,c", ",", 2))
273 // Output: ["a," "b,c"]
276 func ExampleTitle() {
277 // Compare this example to the ToTitle example.
278 fmt
.Println(strings
.Title("her royal highness"))
279 fmt
.Println(strings
.Title("loud noises"))
280 fmt
.Println(strings
.Title("хлеб"))
282 // Her Royal Highness
287 func ExampleToTitle() {
288 // Compare this example to the Title example.
289 fmt
.Println(strings
.ToTitle("her royal highness"))
290 fmt
.Println(strings
.ToTitle("loud noises"))
291 fmt
.Println(strings
.ToTitle("хлеб"))
293 // HER ROYAL HIGHNESS
298 func ExampleToTitleSpecial() {
299 fmt
.Println(strings
.ToTitleSpecial(unicode
.TurkishCase
, "dünyanın ilk borsa yapısı Aizonai kabul edilir"))
301 // DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR
305 rot13
:= func(r rune
) rune
{
307 case r
>= 'A' && r
<= 'Z':
308 return 'A' + (r
-'A'+13)%26
309 case r
>= 'a' && r
<= 'z':
310 return 'a' + (r
-'a'+13)%26
314 fmt
.Println(strings
.Map(rot13
, "'Twas brillig and the slithy gopher..."))
315 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
318 func ExampleNewReplacer() {
319 r
:= strings
.NewReplacer("<", "<", ">", ">")
320 fmt
.Println(r
.Replace("This is <b>HTML</b>!"))
321 // Output: This is <b>HTML</b>!
324 func ExampleToUpper() {
325 fmt
.Println(strings
.ToUpper("Gopher"))
329 func ExampleToUpperSpecial() {
330 fmt
.Println(strings
.ToUpperSpecial(unicode
.TurkishCase
, "örnek iş"))
334 func ExampleToLower() {
335 fmt
.Println(strings
.ToLower("Gopher"))
339 func ExampleToLowerSpecial() {
340 fmt
.Println(strings
.ToLowerSpecial(unicode
.TurkishCase
, "Önnek İş"))
345 fmt
.Print(strings
.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
346 // Output: Hello, Gophers
349 func ExampleTrimSpace() {
350 fmt
.Println(strings
.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
351 // Output: Hello, Gophers
354 func ExampleTrimPrefix() {
355 var s
= "¡¡¡Hello, Gophers!!!"
356 s
= strings
.TrimPrefix(s
, "¡¡¡Hello, ")
357 s
= strings
.TrimPrefix(s
, "¡¡¡Howdy, ")
359 // Output: Gophers!!!
362 func ExampleTrimSuffix() {
363 var s
= "¡¡¡Hello, Gophers!!!"
364 s
= strings
.TrimSuffix(s
, ", Gophers!!!")
365 s
= strings
.TrimSuffix(s
, ", Marmots!!!")
370 func ExampleTrimFunc() {
371 fmt
.Print(strings
.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune
) bool {
372 return !unicode
.IsLetter(r
) && !unicode
.IsNumber(r
)
374 // Output: Hello, Gophers
377 func ExampleTrimLeft() {
378 fmt
.Print(strings
.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡"))
379 // Output: Hello, Gophers!!!
382 func ExampleTrimLeftFunc() {
383 fmt
.Print(strings
.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune
) bool {
384 return !unicode
.IsLetter(r
) && !unicode
.IsNumber(r
)
386 // Output: Hello, Gophers!!!
389 func ExampleTrimRight() {
390 fmt
.Print(strings
.TrimRight("¡¡¡Hello, Gophers!!!", "!¡"))
391 // Output: ¡¡¡Hello, Gophers
394 func ExampleTrimRightFunc() {
395 fmt
.Print(strings
.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune
) bool {
396 return !unicode
.IsLetter(r
) && !unicode
.IsNumber(r
)
398 // Output: ¡¡¡Hello, Gophers