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