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