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