Source file
src/strings/example_test.go
1
2
3
4
5 package strings_test
6
7 import (
8 "fmt"
9 "strings"
10 "unicode"
11 )
12
13 func ExampleBuilder() {
14 var b strings.Builder
15 for i := 3; i >= 1; i-- {
16 fmt.Fprintf(&b, "%d...", i)
17 }
18 b.WriteString("ignition")
19 fmt.Println(b.String())
20
21
22 }
23
24 func ExampleCompare() {
25 fmt.Println(strings.Compare("a", "b"))
26 fmt.Println(strings.Compare("a", "a"))
27 fmt.Println(strings.Compare("b", "a"))
28
29
30
31
32 }
33
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("", ""))
39
40
41
42
43
44 }
45
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("", ""))
53
54
55
56
57
58
59
60 }
61
62 func ExampleContainsRune() {
63
64
65 fmt.Println(strings.ContainsRune("aardvark", 97))
66 fmt.Println(strings.ContainsRune("timeout", 97))
67
68
69
70 }
71
72 func ExampleCount() {
73 fmt.Println(strings.Count("cheese", "e"))
74 fmt.Println(strings.Count("five", ""))
75
76
77
78 }
79
80 func ExampleCut() {
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)
84 }
85 show("Gopher", "Go")
86 show("Gopher", "ph")
87 show("Gopher", "er")
88 show("Gopher", "Badger")
89
90
91
92
93
94 }
95
96 func ExampleEqualFold() {
97 fmt.Println(strings.EqualFold("Go", "go"))
98 fmt.Println(strings.EqualFold("AB", "ab"))
99 fmt.Println(strings.EqualFold("ß", "ss"))
100
101
102
103
104 }
105
106 func ExampleFields() {
107 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
108
109 }
110
111 func ExampleFieldsFunc() {
112 f := func(c rune) bool {
113 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
114 }
115 fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))
116
117 }
118
119 func ExampleHasPrefix() {
120 fmt.Println(strings.HasPrefix("Gopher", "Go"))
121 fmt.Println(strings.HasPrefix("Gopher", "C"))
122 fmt.Println(strings.HasPrefix("Gopher", ""))
123
124
125
126
127 }
128
129 func ExampleHasSuffix() {
130 fmt.Println(strings.HasSuffix("Amigo", "go"))
131 fmt.Println(strings.HasSuffix("Amigo", "O"))
132 fmt.Println(strings.HasSuffix("Amigo", "Ami"))
133 fmt.Println(strings.HasSuffix("Amigo", ""))
134
135
136
137
138
139 }
140
141 func ExampleIndex() {
142 fmt.Println(strings.Index("chicken", "ken"))
143 fmt.Println(strings.Index("chicken", "dmr"))
144
145
146
147 }
148
149 func ExampleIndexFunc() {
150 f := func(c rune) bool {
151 return unicode.Is(unicode.Han, c)
152 }
153 fmt.Println(strings.IndexFunc("Hello, 世界", f))
154 fmt.Println(strings.IndexFunc("Hello, world", f))
155
156
157
158 }
159
160 func ExampleIndexAny() {
161 fmt.Println(strings.IndexAny("chicken", "aeiouy"))
162 fmt.Println(strings.IndexAny("crwth", "aeiouy"))
163
164
165
166 }
167
168 func ExampleIndexByte() {
169 fmt.Println(strings.IndexByte("golang", 'g'))
170 fmt.Println(strings.IndexByte("gophers", 'h'))
171 fmt.Println(strings.IndexByte("golang", 'x'))
172
173
174
175
176 }
177 func ExampleIndexRune() {
178 fmt.Println(strings.IndexRune("chicken", 'k'))
179 fmt.Println(strings.IndexRune("chicken", 'd'))
180
181
182
183 }
184
185 func ExampleLastIndex() {
186 fmt.Println(strings.Index("go gopher", "go"))
187 fmt.Println(strings.LastIndex("go gopher", "go"))
188 fmt.Println(strings.LastIndex("go gopher", "rodent"))
189
190
191
192
193 }
194
195 func ExampleLastIndexAny() {
196 fmt.Println(strings.LastIndexAny("go gopher", "go"))
197 fmt.Println(strings.LastIndexAny("go gopher", "rodent"))
198 fmt.Println(strings.LastIndexAny("go gopher", "fail"))
199
200
201
202
203 }
204
205 func ExampleLastIndexByte() {
206 fmt.Println(strings.LastIndexByte("Hello, world", 'l'))
207 fmt.Println(strings.LastIndexByte("Hello, world", 'o'))
208 fmt.Println(strings.LastIndexByte("Hello, world", 'x'))
209
210
211
212
213 }
214
215 func ExampleLastIndexFunc() {
216 fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber))
217 fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber))
218 fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber))
219
220
221
222
223 }
224
225 func ExampleJoin() {
226 s := []string{"foo", "bar", "baz"}
227 fmt.Println(strings.Join(s, ", "))
228
229 }
230
231 func ExampleRepeat() {
232 fmt.Println("ba" + strings.Repeat("na", 2))
233
234 }
235
236 func ExampleReplace() {
237 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
238 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
239
240
241
242 }
243
244 func ExampleReplaceAll() {
245 fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo"))
246
247
248 }
249
250 func ExampleSplit() {
251 fmt.Printf("%q\n", strings.Split("a,b,c", ","))
252 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
253 fmt.Printf("%q\n", strings.Split(" xyz ", ""))
254 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
255
256
257
258
259
260 }
261
262 func ExampleSplitN() {
263 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
264 z := strings.SplitN("a,b,c", ",", 0)
265 fmt.Printf("%q (nil = %v)\n", z, z == nil)
266
267
268
269 }
270
271 func ExampleSplitAfter() {
272 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
273
274 }
275
276 func ExampleSplitAfterN() {
277 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
278
279 }
280
281 func ExampleTitle() {
282
283 fmt.Println(strings.Title("her royal highness"))
284 fmt.Println(strings.Title("loud noises"))
285 fmt.Println(strings.Title("хлеб"))
286
287
288
289
290 }
291
292 func ExampleToTitle() {
293
294 fmt.Println(strings.ToTitle("her royal highness"))
295 fmt.Println(strings.ToTitle("loud noises"))
296 fmt.Println(strings.ToTitle("хлеб"))
297
298
299
300
301 }
302
303 func ExampleToTitleSpecial() {
304 fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir"))
305
306
307 }
308
309 func ExampleMap() {
310 rot13 := func(r rune) rune {
311 switch {
312 case r >= 'A' && r <= 'Z':
313 return 'A' + (r-'A'+13)%26
314 case r >= 'a' && r <= 'z':
315 return 'a' + (r-'a'+13)%26
316 }
317 return r
318 }
319 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
320
321 }
322
323 func ExampleNewReplacer() {
324 r := strings.NewReplacer("<", "<", ">", ">")
325 fmt.Println(r.Replace("This is <b>HTML</b>!"))
326
327 }
328
329 func ExampleToUpper() {
330 fmt.Println(strings.ToUpper("Gopher"))
331
332 }
333
334 func ExampleToUpperSpecial() {
335 fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş"))
336
337 }
338
339 func ExampleToLower() {
340 fmt.Println(strings.ToLower("Gopher"))
341
342 }
343
344 func ExampleToLowerSpecial() {
345 fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş"))
346
347 }
348
349 func ExampleTrim() {
350 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
351
352 }
353
354 func ExampleTrimSpace() {
355 fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
356
357 }
358
359 func ExampleTrimPrefix() {
360 var s = "¡¡¡Hello, Gophers!!!"
361 s = strings.TrimPrefix(s, "¡¡¡Hello, ")
362 s = strings.TrimPrefix(s, "¡¡¡Howdy, ")
363 fmt.Print(s)
364
365 }
366
367 func ExampleTrimSuffix() {
368 var s = "¡¡¡Hello, Gophers!!!"
369 s = strings.TrimSuffix(s, ", Gophers!!!")
370 s = strings.TrimSuffix(s, ", Marmots!!!")
371 fmt.Print(s)
372
373 }
374
375 func ExampleTrimFunc() {
376 fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
377 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
378 }))
379
380 }
381
382 func ExampleTrimLeft() {
383 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡"))
384
385 }
386
387 func ExampleTrimLeftFunc() {
388 fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
389 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
390 }))
391
392 }
393
394 func ExampleTrimRight() {
395 fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡"))
396
397 }
398
399 func ExampleTrimRightFunc() {
400 fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
401 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
402 }))
403
404 }
405
View as plain text