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
99 }
100
101 func ExampleFields() {
102 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
103
104 }
105
106 func ExampleFieldsFunc() {
107 f := func(c rune) bool {
108 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
109 }
110 fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))
111
112 }
113
114 func ExampleHasPrefix() {
115 fmt.Println(strings.HasPrefix("Gopher", "Go"))
116 fmt.Println(strings.HasPrefix("Gopher", "C"))
117 fmt.Println(strings.HasPrefix("Gopher", ""))
118
119
120
121
122 }
123
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", ""))
129
130
131
132
133
134 }
135
136 func ExampleIndex() {
137 fmt.Println(strings.Index("chicken", "ken"))
138 fmt.Println(strings.Index("chicken", "dmr"))
139
140
141
142 }
143
144 func ExampleIndexFunc() {
145 f := func(c rune) bool {
146 return unicode.Is(unicode.Han, c)
147 }
148 fmt.Println(strings.IndexFunc("Hello, 世界", f))
149 fmt.Println(strings.IndexFunc("Hello, world", f))
150
151
152
153 }
154
155 func ExampleIndexAny() {
156 fmt.Println(strings.IndexAny("chicken", "aeiouy"))
157 fmt.Println(strings.IndexAny("crwth", "aeiouy"))
158
159
160
161 }
162
163 func ExampleIndexByte() {
164 fmt.Println(strings.IndexByte("golang", 'g'))
165 fmt.Println(strings.IndexByte("gophers", 'h'))
166 fmt.Println(strings.IndexByte("golang", 'x'))
167
168
169
170
171 }
172 func ExampleIndexRune() {
173 fmt.Println(strings.IndexRune("chicken", 'k'))
174 fmt.Println(strings.IndexRune("chicken", 'd'))
175
176
177
178 }
179
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"))
184
185
186
187
188 }
189
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"))
194
195
196
197
198 }
199
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'))
204
205
206
207
208 }
209
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))
214
215
216
217
218 }
219
220 func ExampleJoin() {
221 s := []string{"foo", "bar", "baz"}
222 fmt.Println(strings.Join(s, ", "))
223
224 }
225
226 func ExampleRepeat() {
227 fmt.Println("ba" + strings.Repeat("na", 2))
228
229 }
230
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))
234
235
236
237 }
238
239 func ExampleReplaceAll() {
240 fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo"))
241
242
243 }
244
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"))
250
251
252
253
254
255 }
256
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)
261
262
263
264 }
265
266 func ExampleSplitAfter() {
267 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
268
269 }
270
271 func ExampleSplitAfterN() {
272 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
273
274 }
275
276 func ExampleTitle() {
277
278 fmt.Println(strings.Title("her royal highness"))
279 fmt.Println(strings.Title("loud noises"))
280 fmt.Println(strings.Title("хлеб"))
281
282
283
284
285 }
286
287 func ExampleToTitle() {
288
289 fmt.Println(strings.ToTitle("her royal highness"))
290 fmt.Println(strings.ToTitle("loud noises"))
291 fmt.Println(strings.ToTitle("хлеб"))
292
293
294
295
296 }
297
298 func ExampleToTitleSpecial() {
299 fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir"))
300
301
302 }
303
304 func ExampleMap() {
305 rot13 := func(r rune) rune {
306 switch {
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
311 }
312 return r
313 }
314 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
315
316 }
317
318 func ExampleNewReplacer() {
319 r := strings.NewReplacer("<", "<", ">", ">")
320 fmt.Println(r.Replace("This is <b>HTML</b>!"))
321
322 }
323
324 func ExampleToUpper() {
325 fmt.Println(strings.ToUpper("Gopher"))
326
327 }
328
329 func ExampleToUpperSpecial() {
330 fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş"))
331
332 }
333
334 func ExampleToLower() {
335 fmt.Println(strings.ToLower("Gopher"))
336
337 }
338
339 func ExampleToLowerSpecial() {
340 fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş"))
341
342 }
343
344 func ExampleTrim() {
345 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
346
347 }
348
349 func ExampleTrimSpace() {
350 fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
351
352 }
353
354 func ExampleTrimPrefix() {
355 var s = "¡¡¡Hello, Gophers!!!"
356 s = strings.TrimPrefix(s, "¡¡¡Hello, ")
357 s = strings.TrimPrefix(s, "¡¡¡Howdy, ")
358 fmt.Print(s)
359
360 }
361
362 func ExampleTrimSuffix() {
363 var s = "¡¡¡Hello, Gophers!!!"
364 s = strings.TrimSuffix(s, ", Gophers!!!")
365 s = strings.TrimSuffix(s, ", Marmots!!!")
366 fmt.Print(s)
367
368 }
369
370 func ExampleTrimFunc() {
371 fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
372 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
373 }))
374
375 }
376
377 func ExampleTrimLeft() {
378 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡"))
379
380 }
381
382 func ExampleTrimLeftFunc() {
383 fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
384 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
385 }))
386
387 }
388
389 func ExampleTrimRight() {
390 fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡"))
391
392 }
393
394 func ExampleTrimRightFunc() {
395 fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
396 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
397 }))
398
399 }
400
View as plain text