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.
13 func ExampleFields() {
14 fmt
.Printf("Fields are: %q", strings
.Fields(" foo bar baz "))
15 // Output: Fields are: ["foo" "bar" "baz"]
18 func ExampleFieldsFunc() {
19 f
:= func(c rune
) bool {
20 return !unicode
.IsLetter(c
) && !unicode
.IsNumber(c
)
22 fmt
.Printf("Fields are: %q", strings
.FieldsFunc(" foo1;bar2,baz3...", f
))
23 // Output: Fields are: ["foo1" "bar2" "baz3"]
26 func ExampleCompare() {
27 fmt
.Println(strings
.Compare("a", "b"))
28 fmt
.Println(strings
.Compare("a", "a"))
29 fmt
.Println(strings
.Compare("b", "a"))
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("", ""))
48 func ExampleContainsAny() {
49 fmt
.Println(strings
.ContainsAny("team", "i"))
50 fmt
.Println(strings
.ContainsAny("failure", "u & i"))
51 fmt
.Println(strings
.ContainsAny("foo", ""))
52 fmt
.Println(strings
.ContainsAny("", ""))
60 func ExampleContainsRune() {
61 // Finds whether a string contains a particular Unicode code point.
62 // The code point for the lowercase letter "a", for example, is 97.
63 fmt
.Println(strings
.ContainsRune("aardvark", 97))
64 fmt
.Println(strings
.ContainsRune("timeout", 97))
71 fmt
.Println(strings
.Count("cheese", "e"))
72 fmt
.Println(strings
.Count("five", "")) // before & after each rune
78 func ExampleEqualFold() {
79 fmt
.Println(strings
.EqualFold("Go", "go"))
83 func ExampleHasPrefix() {
84 fmt
.Println(strings
.HasPrefix("Gopher", "Go"))
85 fmt
.Println(strings
.HasPrefix("Gopher", "C"))
86 fmt
.Println(strings
.HasPrefix("Gopher", ""))
93 func ExampleHasSuffix() {
94 fmt
.Println(strings
.HasSuffix("Amigo", "go"))
95 fmt
.Println(strings
.HasSuffix("Amigo", "O"))
96 fmt
.Println(strings
.HasSuffix("Amigo", "Ami"))
97 fmt
.Println(strings
.HasSuffix("Amigo", ""))
105 func ExampleIndex() {
106 fmt
.Println(strings
.Index("chicken", "ken"))
107 fmt
.Println(strings
.Index("chicken", "dmr"))
113 func ExampleIndexFunc() {
114 f
:= func(c rune
) bool {
115 return unicode
.Is(unicode
.Han
, c
)
117 fmt
.Println(strings
.IndexFunc("Hello, 世界", f
))
118 fmt
.Println(strings
.IndexFunc("Hello, world", f
))
124 func ExampleIndexAny() {
125 fmt
.Println(strings
.IndexAny("chicken", "aeiouy"))
126 fmt
.Println(strings
.IndexAny("crwth", "aeiouy"))
132 func ExampleIndexByte() {
133 fmt
.Println(strings
.IndexByte("golang", 'g'))
134 fmt
.Println(strings
.IndexByte("gophers", 'h'))
135 fmt
.Println(strings
.IndexByte("golang", 'x'))
141 func ExampleIndexRune() {
142 fmt
.Println(strings
.IndexRune("chicken", 'k'))
143 fmt
.Println(strings
.IndexRune("chicken", 'd'))
149 func ExampleLastIndex() {
150 fmt
.Println(strings
.Index("go gopher", "go"))
151 fmt
.Println(strings
.LastIndex("go gopher", "go"))
152 fmt
.Println(strings
.LastIndex("go gopher", "rodent"))
159 func ExampleLastIndexAny() {
160 fmt
.Println(strings
.LastIndexAny("go gopher", "go"))
161 fmt
.Println(strings
.LastIndexAny("go gopher", "rodent"))
162 fmt
.Println(strings
.LastIndexAny("go gopher", "fail"))
170 s
:= []string{"foo", "bar", "baz"}
171 fmt
.Println(strings
.Join(s
, ", "))
172 // Output: foo, bar, baz
175 func ExampleRepeat() {
176 fmt
.Println("ba" + strings
.Repeat("na", 2))
180 func ExampleReplace() {
181 fmt
.Println(strings
.Replace("oink oink oink", "k", "ky", 2))
182 fmt
.Println(strings
.Replace("oink oink oink", "oink", "moo", -1))
188 func ExampleSplit() {
189 fmt
.Printf("%q\n", strings
.Split("a,b,c", ","))
190 fmt
.Printf("%q\n", strings
.Split("a man a plan a canal panama", "a "))
191 fmt
.Printf("%q\n", strings
.Split(" xyz ", ""))
192 fmt
.Printf("%q\n", strings
.Split("", "Bernardo O'Higgins"))
195 // ["" "man " "plan " "canal panama"]
196 // [" " "x" "y" "z" " "]
200 func ExampleSplitN() {
201 fmt
.Printf("%q\n", strings
.SplitN("a,b,c", ",", 2))
202 z
:= strings
.SplitN("a,b,c", ",", 0)
203 fmt
.Printf("%q (nil = %v)\n", z
, z
== nil)
209 func ExampleSplitAfter() {
210 fmt
.Printf("%q\n", strings
.SplitAfter("a,b,c", ","))
211 // Output: ["a," "b," "c"]
214 func ExampleSplitAfterN() {
215 fmt
.Printf("%q\n", strings
.SplitAfterN("a,b,c", ",", 2))
216 // Output: ["a," "b,c"]
219 func ExampleTitle() {
220 fmt
.Println(strings
.Title("her royal highness"))
221 // Output: Her Royal Highness
224 func ExampleToTitle() {
225 fmt
.Println(strings
.ToTitle("loud noises"))
226 fmt
.Println(strings
.ToTitle("хлеб"))
233 fmt
.Printf("[%q]", strings
.Trim(" !!! Achtung! Achtung! !!! ", "! "))
234 // Output: ["Achtung! Achtung"]
237 func ExampleTrimFunc() {
238 f
:= func(c rune
) bool {
239 return !unicode
.IsLetter(c
) && !unicode
.IsNumber(c
)
241 fmt
.Printf("[%q]", strings
.TrimFunc(" Achtung1! Achtung2,...", f
))
242 // Output: ["Achtung1! Achtung2"]
246 rot13
:= func(r rune
) rune
{
248 case r
>= 'A' && r
<= 'Z':
249 return 'A' + (r
-'A'+13)%26
250 case r
>= 'a' && r
<= 'z':
251 return 'a' + (r
-'a'+13)%26
255 fmt
.Println(strings
.Map(rot13
, "'Twas brillig and the slithy gopher..."))
256 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
259 func ExampleTrimSpace() {
260 fmt
.Println(strings
.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
261 // Output: a lone gopher
264 func ExampleNewReplacer() {
265 r
:= strings
.NewReplacer("<", "<", ">", ">")
266 fmt
.Println(r
.Replace("This is <b>HTML</b>!"))
267 // Output: This is <b>HTML</b>!
270 func ExampleToUpper() {
271 fmt
.Println(strings
.ToUpper("Gopher"))
275 func ExampleToLower() {
276 fmt
.Println(strings
.ToLower("Gopher"))
280 func ExampleTrimSuffix() {
281 var s
= "Hello, goodbye, etc!"
282 s
= strings
.TrimSuffix(s
, "goodbye, etc!")
283 s
= strings
.TrimSuffix(s
, "planet")
284 fmt
.Print(s
, "world!")
285 // Output: Hello, world!
288 func ExampleTrimPrefix() {
289 var s
= "Goodbye,, world!"
290 s
= strings
.TrimPrefix(s
, "Goodbye,")
291 s
= strings
.TrimPrefix(s
, "Howdy,")
292 fmt
.Print("Hello" + s
)
293 // Output: Hello, world!