Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / libgo / go / strings / example_test.go
blob94aa167f90900c57be37a90d484455f88c7e5468
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.
5 package strings_test
7 import (
8 "fmt"
9 "strings"
10 "unicode"
13 func ExampleBuilder() {
14 var b strings.Builder
15 for i := 3; i >= 1; i-- {
16 fmt.Fprintf(&b, "%d...", i)
18 b.WriteString("ignition")
19 fmt.Println(b.String())
21 // Output: 3...2...1...ignition
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
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
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
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
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
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)
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
96 func ExampleEqualFold() {
97 fmt.Println(strings.EqualFold("Go", "go"))
98 // Output: true
101 func ExampleFields() {
102 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
103 // Output: Fields are: ["foo" "bar" "baz"]
106 func ExampleFieldsFunc() {
107 f := func(c rune) bool {
108 return !unicode.IsLetter(c) && !unicode.IsNumber(c)
110 fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))
111 // Output: Fields are: ["foo1" "bar2" "baz3"]
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
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
136 func ExampleIndex() {
137 fmt.Println(strings.Index("chicken", "ken"))
138 fmt.Println(strings.Index("chicken", "dmr"))
139 // Output:
140 // 4
141 // -1
144 func ExampleIndexFunc() {
145 f := func(c rune) bool {
146 return unicode.Is(unicode.Han, c)
148 fmt.Println(strings.IndexFunc("Hello, 世界", f))
149 fmt.Println(strings.IndexFunc("Hello, world", f))
150 // Output:
151 // 7
152 // -1
155 func ExampleIndexAny() {
156 fmt.Println(strings.IndexAny("chicken", "aeiouy"))
157 fmt.Println(strings.IndexAny("crwth", "aeiouy"))
158 // Output:
159 // 2
160 // -1
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
172 func ExampleIndexRune() {
173 fmt.Println(strings.IndexRune("chicken", 'k'))
174 fmt.Println(strings.IndexRune("chicken", 'd'))
175 // Output:
176 // 4
177 // -1
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
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
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
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
220 func ExampleJoin() {
221 s := []string{"foo", "bar", "baz"}
222 fmt.Println(strings.Join(s, ", "))
223 // Output: foo, bar, baz
226 func ExampleRepeat() {
227 fmt.Println("ba" + strings.Repeat("na", 2))
228 // Output: banana
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
239 func ExampleReplaceAll() {
240 fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo"))
241 // Output:
242 // moo moo moo
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 // [""]
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)
266 func ExampleSplitAfter() {
267 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
268 // Output: ["a," "b," "c"]
271 func ExampleSplitAfterN() {
272 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
273 // Output: ["a," "b,c"]
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 // Хлеб
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 // ХЛЕБ
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
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
312 return r
314 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
315 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
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;!
324 func ExampleToUpper() {
325 fmt.Println(strings.ToUpper("Gopher"))
326 // Output: GOPHER
329 func ExampleToUpperSpecial() {
330 fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş"))
331 // Output: ÖRNEK İŞ
334 func ExampleToLower() {
335 fmt.Println(strings.ToLower("Gopher"))
336 // Output: gopher
339 func ExampleToLowerSpecial() {
340 fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş"))
341 // Output: önnek iş
344 func ExampleTrim() {
345 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡"))
346 // Output: Hello, Gophers
349 func ExampleTrimSpace() {
350 fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n"))
351 // Output: Hello, Gophers
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!!!
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
370 func ExampleTrimFunc() {
371 fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
372 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
374 // Output: Hello, Gophers
377 func ExampleTrimLeft() {
378 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡"))
379 // Output: Hello, Gophers!!!
382 func ExampleTrimLeftFunc() {
383 fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
384 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
386 // Output: Hello, Gophers!!!
389 func ExampleTrimRight() {
390 fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡"))
391 // Output: ¡¡¡Hello, Gophers
394 func ExampleTrimRightFunc() {
395 fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool {
396 return !unicode.IsLetter(r) && !unicode.IsNumber(r)
398 // Output: ¡¡¡Hello, Gophers