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.
12 func ExampleFields() {
13 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
14 // Output: Fields are: ["foo" "bar" "baz"]
17 func ExampleContains() {
18 fmt.Println(strings.Contains("seafood", "foo"))
19 fmt.Println(strings.Contains("seafood", "bar"))
20 fmt.Println(strings.Contains("seafood", ""))
21 fmt.Println(strings.Contains("", ""))
29 func ExampleContainsAny() {
30 fmt.Println(strings.ContainsAny("team", "i"))
31 fmt.Println(strings.ContainsAny("failure", "u & i"))
32 fmt.Println(strings.ContainsAny("foo", ""))
33 fmt.Println(strings.ContainsAny("", ""))
42 fmt.Println(strings.Count("cheese", "e"))
43 fmt.Println(strings.Count("five", "")) // before & after each rune
50 func ExampleEqualFold() {
51 fmt.Println(strings.EqualFold("Go", "go"))
56 fmt.Println(strings.Index("chicken", "ken"))
57 fmt.Println(strings.Index("chicken", "dmr"))
64 fmt.Println(strings.IndexRune("chicken", 'k'))
65 fmt.Println(strings.IndexRune("chicken", 'd'))
71 func ExampleLastIndex() {
72 fmt.Println(strings.Index("go gopher", "go"))
73 fmt.Println(strings.LastIndex("go gopher", "go"))
74 fmt.Println(strings.LastIndex("go gopher", "rodent"))
82 s := []string{"foo", "bar", "baz"}
83 fmt.Println(strings.Join(s, ", "))
84 // Output: foo, bar, baz
87 func ExampleRepeat() {
88 fmt.Println("ba" + strings.Repeat("na", 2))
92 func ExampleReplace() {
93 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
94 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
100 func ExampleSplit() {
101 fmt.Printf("%q\n", strings.Split("a,b,c", ","))
102 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
103 fmt.Printf("%q\n", strings.Split(" xyz ", ""))
104 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
107 // ["" "man " "plan " "canal panama"]
108 // [" " "x" "y" "z" " "]
112 func ExampleSplitN() {
113 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
114 z := strings.SplitN("a,b,c", ",", 0)
115 fmt.Printf("%q (nil = %v)\n", z, z == nil)
121 func ExampleSplitAfter() {
122 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
123 // Output: ["a," "b," "c"]
126 func ExampleSplitAfterN() {
127 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
128 // Output: ["a," "b,c"]
131 func ExampleTitle() {
132 fmt.Println(strings.Title("her royal highness"))
133 // Output: Her Royal Highness
136 func ExampleToTitle() {
137 fmt.Println(strings.ToTitle("loud noises"))
138 fmt.Println(strings.ToTitle("хлеб"))
145 fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
146 // Output: ["Achtung"]
150 rot13 := func(r rune) rune {
152 case r >= 'A' && r <= 'Z':
153 return 'A' + (r-'A'+13)%26
154 case r >= 'a' && r <= 'z':
155 return 'a' + (r-'a'+13)%26
159 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
160 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
163 func ExampleTrimSpace() {
164 fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
165 // Output: a lone gopher
168 func ExampleNewReplacer() {
169 r := strings.NewReplacer("<", "<", ">", ">")
170 fmt.Println(r.Replace("This is <b>HTML</b>!"))
171 // Output: This is <b>HTML</b>!
174 func ExampleToUpper() {
175 fmt.Println(strings.ToUpper("Gopher"))
179 func ExampleToLower() {
180 fmt.Println(strings.ToLower("Gopher"))