OSDN Git Service

Update Go compiler, library, and testsuite on gcc 4.7 branch.
[pf3gnuchains/gcc-fork.git] / libgo / go / strings / example_test.go
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 )
11
12 func ExampleFields() {
13         fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
14         // Output: Fields are: ["foo" "bar" "baz"]
15 }
16
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("", ""))
22         // Output:
23         // true
24         // false
25         // true
26         // true
27 }
28
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("", ""))
34         // Output:
35         // false
36         // true
37         // false
38         // false
39 }
40
41 func ExampleCount() {
42         fmt.Println(strings.Count("cheese", "e"))
43         fmt.Println(strings.Count("five", "")) // before & after each rune
44
45         // Output:
46         // 3
47         // 5
48 }
49
50 func ExampleEqualFold() {
51         fmt.Println(strings.EqualFold("Go", "go"))
52         // Output: true
53 }
54
55 func ExampleIndex() {
56         fmt.Println(strings.Index("chicken", "ken"))
57         fmt.Println(strings.Index("chicken", "dmr"))
58         // Output:
59         // 4
60         // -1
61 }
62
63 func ExampleRune() {
64         fmt.Println(strings.IndexRune("chicken", 'k'))
65         fmt.Println(strings.IndexRune("chicken", 'd'))
66         // Output:
67         // 4
68         // -1
69 }
70
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"))
75         // Output:
76         // 0
77         // 3
78         // -1
79 }
80
81 func ExampleJoin() {
82         s := []string{"foo", "bar", "baz"}
83         fmt.Println(strings.Join(s, ", "))
84         // Output: foo, bar, baz
85 }
86
87 func ExampleRepeat() {
88         fmt.Println("ba" + strings.Repeat("na", 2))
89         // Output: banana
90 }
91
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))
95         // Output:
96         // oinky oinky oink
97         // moo moo moo
98 }
99
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"))
105         // Output:
106         // ["a" "b" "c"]
107         // ["" "man " "plan " "canal panama"]
108         // [" " "x" "y" "z" " "]
109         // [""]
110 }
111
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)
116         // Output:
117         // ["a" "b,c"]
118         // [] (nil = true)
119 }
120
121 func ExampleSplitAfter() {
122         fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
123         // Output: ["a," "b," "c"]
124 }
125
126 func ExampleSplitAfterN() {
127         fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
128         // Output: ["a," "b,c"]
129 }
130
131 func ExampleTitle() {
132         fmt.Println(strings.Title("her royal highness"))
133         // Output: Her Royal Highness
134 }
135
136 func ExampleToTitle() {
137         fmt.Println(strings.ToTitle("loud noises"))
138         fmt.Println(strings.ToTitle("хлеб"))
139         // Output:
140         // LOUD NOISES
141         // ХЛЕБ
142 }
143
144 func ExampleTrim() {
145         fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
146         // Output: ["Achtung"]
147 }
148
149 func ExampleMap() {
150         rot13 := func(r rune) rune {
151                 switch {
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
156                 }
157                 return r
158         }
159         fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
160         // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
161 }
162
163 func ExampleTrimSpace() {
164         fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
165         // Output: a lone gopher
166 }
167
168 func ExampleNewReplacer() {
169         r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
170         fmt.Println(r.Replace("This is <b>HTML</b>!"))
171         // Output: This is &lt;b&gt;HTML&lt;/b&gt;!
172 }
173
174 func ExampleToUpper() {
175         fmt.Println(strings.ToUpper("Gopher"))
176         // Output: GOPHER
177 }
178
179 func ExampleToLower() {
180         fmt.Println(strings.ToLower("Gopher"))
181         // Output: gopher
182 }