Debian Codegraph @

libgo/go/strings/example_test.go

raw
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
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 // Output: 3...2...1...ignition
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 // Output:
29 // -1
30 // 0
31 // 1
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 // Output:
40 // true
41 // false
42 // true
43 // true
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 // Output:
54 // false
55 // true
56 // true
57 // true
58 // false
59 // false
60 }
61
62 func ExampleContainsRune() {
63 // Finds whether a string contains a particular Unicode code point.
64 // The code point for the lowercase letter "a", for example, is 97.
65 fmt.Println(strings.ContainsRune("aardvark", 97))
66 fmt.Println(strings.ContainsRune("timeout", 97))
67 // Output:
68 // true
69 // false
70 }
71
72 func ExampleCount() {
73 fmt.Println(strings.Count("cheese", "e"))
74 fmt.Println(strings.Count("five", "")) // before & after each rune
75 // Output:
76 // 3
77 // 5
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 // Output:
90 // Cut("Gopher", "Go") = "", "pher", true
91 // Cut("Gopher", "ph") = "Go", "er", true
92 // Cut("Gopher", "er") = "Goph", "", true
93 // Cut("Gopher", "Badger") = "Gopher", "", false
94 }
95
96 func ExampleEqualFold() {
97 fmt.Println(strings.EqualFold("Go", "go"))
98 // Output: true
99 }
100
101 func ExampleFields() {
102 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
103 // Output: Fields are: ["foo" "bar" "baz"]
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 // Output: Fields are: ["foo1" "bar2" "baz3"]
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 // Output:
119 // true
120 // false
121 // true
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 // Output:
130 // true
131 // false
132 // false
133 // true
134 }
135
136 func ExampleIndex() {
137 fmt.Println(strings.Index("chicken", "ken"))
138 fmt.Println(strings.Index("chicken", "dmr"))
139 // Output:
140 // 4
141 // -1
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 // Output:
151 // 7
152 // -1
153 }
154
155 func ExampleIndexAny() {
156 fmt.Println(strings.IndexAny("chicken", "aeiouy"))
157 fmt.Println(strings.IndexAny("crwth", "aeiouy"))
158 // Output:
159 // 2
160 // -1
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 // Output:
168 // 0
169 // 3
170 // -1
171 }
172 func ExampleIndexRune() {
173 fmt.Println(strings.IndexRune("chicken", 'k'))
174 fmt.Println(strings.IndexRune("chicken", 'd'))
175 // Output:
176 // 4
177 // -1
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 // Output:
185 // 0
186 // 3
187 // -1
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 // Output:
195 // 4
196 // 8
197 // -1
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 // Output:
205 // 10
206 // 8
207 // -1
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 // Output:
215 // 5
216 // 2
217 // -1
218 }
219
220 func ExampleJoin() {
221 s := []string{"foo", "bar", "baz"}
222 fmt.Println(strings.Join(s, ", "))
223 // Output: foo, bar, baz
224 }
225
226 func ExampleRepeat() {
227 fmt.Println("ba" + strings.Repeat("na", 2))
228 // Output: banana
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 // Output:
235 // oinky oinky oink
236 // moo moo moo
237 }
238
239 func ExampleReplaceAll() {
240 fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo"))
241 // Output:
242 // moo moo moo
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 // Output:
251 // ["a" "b" "c"]
252 // ["" "man " "plan " "canal panama"]
253 // [" " "x" "y" "z" " "]
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 // Output:
262 // ["a" "b,c"]
263 // [] (nil = true)
264 }
265
266 func ExampleSplitAfter() {
267 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
268 // Output: ["a," "b," "c"]
269 }
270
271 func ExampleSplitAfterN() {
272 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
273 // Output: ["a," "b,c"]
274 }
275
276 func ExampleTitle() {
277 // Compare this example to the ToTitle example.
278 fmt.Println(strings.Title("her royal highness"))
279 fmt.Println(strings.Title("loud noises"))
280 fmt.Println(strings.Title("хлеб"))
281 // Output:
282 // Her Royal Highness
283 // Loud Noises
284 // Хлеб
285 }
286
287 func ExampleToTitle() {
288 // Compare this example to the Title example.
289 fmt.Println(strings.ToTitle("her royal highness"))
290 fmt.Println(strings.ToTitle("loud noises"))
291 fmt.Println(strings.ToTitle("хлеб"))
292 // Output:
293 // HER ROYAL HIGHNESS
294 // LOUD NOISES
295 // ХЛЕБ
296 }
297
298 func ExampleToTitleSpecial() {
299 fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir"))
300 // Output:
301 // DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR
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 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
316 }
317
318 func ExampleNewReplacer() {
319 r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
320 fmt.Println(r.Replace("This is <b>HTML</b>!"))
321 // Output: This is &lt;b&gt;HTML&lt;/b&gt;!
322 }
323
324 func ExampleToUpper() {
325 fmt.Println(strings.ToUpper("Gopher"))
326 // Output: GOPHER
327 }
328
329 func ExampleToUpperSpecial() {
330 fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş"))
331 // Output: ÖRNEK İŞ
332 }
333
334 func ExampleToLower() {
335 fmt.Println(strings.ToLower("Gopher"))
336 // Output: gopher
337 }
338
339 func ExampleToLowerSpecial() {
340 fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş"))
341 // Output: önnek iş
342 }
343
344 func ExampleTrim() {
345 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
346 // Output: Hello, Gophers
347 }
348
349 func ExampleTrimSpace() {
350 fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
351 // Output: Hello, Gophers
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 // Output: Gophers!!!
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 // Output: ¡¡¡Hello
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 // Output: Hello, Gophers
375 }
376
377 func ExampleTrimLeft() {
378 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡"))
379 // Output: Hello, Gophers!!!
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 // Output: Hello, Gophers!!!
387 }
388
389 func ExampleTrimRight() {
390 fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡"))
391 // Output: ¡¡¡Hello, Gophers
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 // Output: ¡¡¡Hello, Gophers
399 }