blob: 7243e16b127086e4931962c0536a45fba8d01792 [file] [log] [blame]
Brad Fitzpatrick10f1b6a2012-02-03 11:17:55 -08001// 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
5package strings_test
6
7import (
8 "fmt"
9 "strings"
Robin Eklinda6ebc882013-12-16 09:43:03 -080010 "unicode"
Brad Fitzpatrick10f1b6a2012-02-03 11:17:55 -080011)
12
Brad Fitzpatrick10f1b6a2012-02-03 11:17:55 -080013func ExampleFields() {
14 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
Andrew Gerrand11e113d2012-02-16 11:50:28 +110015 // Output: Fields are: ["foo" "bar" "baz"]
Brad Fitzpatrick10f1b6a2012-02-03 11:17:55 -080016}
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110017
Robin Eklinda6ebc882013-12-16 09:43:03 -080018func 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 // Output: Fields are: ["foo1" "bar2" "baz3"]
24}
25
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110026func ExampleContains() {
27 fmt.Println(strings.Contains("seafood", "foo"))
28 fmt.Println(strings.Contains("seafood", "bar"))
29 fmt.Println(strings.Contains("seafood", ""))
30 fmt.Println(strings.Contains("", ""))
Andrew Gerrand11e113d2012-02-16 11:50:28 +110031 // Output:
32 // true
33 // false
34 // true
35 // true
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110036}
37
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110038func ExampleContainsAny() {
39 fmt.Println(strings.ContainsAny("team", "i"))
40 fmt.Println(strings.ContainsAny("failure", "u & i"))
41 fmt.Println(strings.ContainsAny("foo", ""))
42 fmt.Println(strings.ContainsAny("", ""))
Andrew Gerrand11e113d2012-02-16 11:50:28 +110043 // Output:
44 // false
45 // true
46 // false
47 // false
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110048}
49
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110050func ExampleCount() {
51 fmt.Println(strings.Count("cheese", "e"))
52 fmt.Println(strings.Count("five", "")) // before & after each rune
Andrew Gerrand11e113d2012-02-16 11:50:28 +110053 // Output:
54 // 3
55 // 5
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110056}
57
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110058func ExampleEqualFold() {
59 fmt.Println(strings.EqualFold("Go", "go"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +110060 // Output: true
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110061}
62
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110063func ExampleIndex() {
64 fmt.Println(strings.Index("chicken", "ken"))
65 fmt.Println(strings.Index("chicken", "dmr"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +110066 // Output:
67 // 4
68 // -1
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110069}
70
Robin Eklindab9b2ae2013-12-16 10:44:23 -080071func ExampleIndexFunc() {
72 f := func(c rune) bool {
73 return unicode.Is(unicode.Han, c)
74 }
75 fmt.Println(strings.IndexFunc("Hello, 世界", f))
76 fmt.Println(strings.IndexFunc("Hello, world", f))
77 // Output:
78 // 7
79 // -1
80}
81
Daniel Lidén4e23b692013-12-16 10:50:56 -080082func ExampleIndexAny() {
83 fmt.Println(strings.IndexAny("chicken", "aeiouy"))
84 fmt.Println(strings.IndexAny("crwth", "aeiouy"))
85 // Output:
86 // 2
87 // -1
88}
89
Volker Dobler06e18ca52012-03-05 22:19:51 +110090func ExampleIndexRune() {
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110091 fmt.Println(strings.IndexRune("chicken", 'k'))
92 fmt.Println(strings.IndexRune("chicken", 'd'))
Andrew Gerrand11e113d2012-02-16 11:50:28 +110093 // Output:
94 // 4
95 // -1
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110096}
97
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +110098func ExampleLastIndex() {
99 fmt.Println(strings.Index("go gopher", "go"))
100 fmt.Println(strings.LastIndex("go gopher", "go"))
101 fmt.Println(strings.LastIndex("go gopher", "rodent"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100102 // Output:
103 // 0
104 // 3
105 // -1
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100106}
107
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100108func ExampleJoin() {
109 s := []string{"foo", "bar", "baz"}
110 fmt.Println(strings.Join(s, ", "))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100111 // Output: foo, bar, baz
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100112}
113
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100114func ExampleRepeat() {
115 fmt.Println("ba" + strings.Repeat("na", 2))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100116 // Output: banana
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100117}
118
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100119func ExampleReplace() {
120 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
121 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100122 // Output:
123 // oinky oinky oink
124 // moo moo moo
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100125}
126
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100127func ExampleSplit() {
128 fmt.Printf("%q\n", strings.Split("a,b,c", ","))
129 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
130 fmt.Printf("%q\n", strings.Split(" xyz ", ""))
Andrew Gerrandd6a36502012-02-15 13:38:28 +1100131 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100132 // Output:
133 // ["a" "b" "c"]
134 // ["" "man " "plan " "canal panama"]
135 // [" " "x" "y" "z" " "]
136 // [""]
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100137}
138
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100139func ExampleSplitN() {
140 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
141 z := strings.SplitN("a,b,c", ",", 0)
142 fmt.Printf("%q (nil = %v)\n", z, z == nil)
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100143 // Output:
144 // ["a" "b,c"]
145 // [] (nil = true)
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100146}
147
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100148func ExampleSplitAfter() {
149 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100150 // Output: ["a," "b," "c"]
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100151}
152
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100153func ExampleSplitAfterN() {
154 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100155 // Output: ["a," "b,c"]
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100156}
157
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100158func ExampleTitle() {
159 fmt.Println(strings.Title("her royal highness"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100160 // Output: Her Royal Highness
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100161}
162
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100163func ExampleToTitle() {
164 fmt.Println(strings.ToTitle("loud noises"))
165 fmt.Println(strings.ToTitle("хлеб"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100166 // Output:
167 // LOUD NOISES
168 // ХЛЕБ
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100169}
170
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100171func ExampleTrim() {
Robin Eklind7b53e322013-12-16 10:40:28 -0800172 fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! "))
173 // Output: ["Achtung! Achtung"]
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100174}
175
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100176func ExampleMap() {
177 rot13 := func(r rune) rune {
178 switch {
179 case r >= 'A' && r <= 'Z':
180 return 'A' + (r-'A'+13)%26
181 case r >= 'a' && r <= 'z':
182 return 'a' + (r-'a'+13)%26
183 }
184 return r
185 }
186 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100187 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100188}
189
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100190func ExampleTrimSpace() {
191 fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100192 // Output: a lone gopher
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100193}
194
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100195func ExampleNewReplacer() {
196 r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
197 fmt.Println(r.Replace("This is <b>HTML</b>!"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100198 // Output: This is &lt;b&gt;HTML&lt;/b&gt;!
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100199}
200
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100201func ExampleToUpper() {
202 fmt.Println(strings.ToUpper("Gopher"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100203 // Output: GOPHER
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100204}
205
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100206func ExampleToLower() {
207 fmt.Println(strings.ToLower("Gopher"))
Andrew Gerrand11e113d2012-02-16 11:50:28 +1100208 // Output: gopher
Brad Fitzpatrick71c8b822012-02-13 14:05:57 +1100209}
Brad Fitzpatricke515d802013-02-01 08:41:25 -0800210
211func ExampleTrimSuffix() {
212 var s = "Hello, goodbye, etc!"
213 s = strings.TrimSuffix(s, "goodbye, etc!")
214 s = strings.TrimSuffix(s, "planet")
215 fmt.Print(s, "world!")
216 // Output: Hello, world!
217}
218
219func ExampleTrimPrefix() {
220 var s = "Goodbye,, world!"
221 s = strings.TrimPrefix(s, "Goodbye,")
222 s = strings.TrimPrefix(s, "Howdy,")
223 fmt.Print("Hello" + s)
224 // Output: Hello, world!
225}