2013-05-24 Nathan Sidwell <nathan@codesourcery.com>
[official-gcc.git] / libgo / go / strings / example_test.go
blob733caf5f2db27bc5d5cbda3d1bb821d9301038f9
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"
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("", ""))
22 // Output:
23 // true
24 // false
25 // true
26 // true
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
41 func ExampleCount() {
42 fmt.Println(strings.Count("cheese", "e"))
43 fmt.Println(strings.Count("five", "")) // before & after each rune
44 // Output:
45 // 3
46 // 5
49 func ExampleEqualFold() {
50 fmt.Println(strings.EqualFold("Go", "go"))
51 // Output: true
54 func ExampleIndex() {
55 fmt.Println(strings.Index("chicken", "ken"))
56 fmt.Println(strings.Index("chicken", "dmr"))
57 // Output:
58 // 4
59 // -1
62 func ExampleIndexRune() {
63 fmt.Println(strings.IndexRune("chicken", 'k'))
64 fmt.Println(strings.IndexRune("chicken", 'd'))
65 // Output:
66 // 4
67 // -1
70 func ExampleLastIndex() {
71 fmt.Println(strings.Index("go gopher", "go"))
72 fmt.Println(strings.LastIndex("go gopher", "go"))
73 fmt.Println(strings.LastIndex("go gopher", "rodent"))
74 // Output:
75 // 0
76 // 3
77 // -1
80 func ExampleJoin() {
81 s := []string{"foo", "bar", "baz"}
82 fmt.Println(strings.Join(s, ", "))
83 // Output: foo, bar, baz
86 func ExampleRepeat() {
87 fmt.Println("ba" + strings.Repeat("na", 2))
88 // Output: banana
91 func ExampleReplace() {
92 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
93 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
94 // Output:
95 // oinky oinky oink
96 // moo moo moo
99 func ExampleSplit() {
100 fmt.Printf("%q\n", strings.Split("a,b,c", ","))
101 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
102 fmt.Printf("%q\n", strings.Split(" xyz ", ""))
103 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
104 // Output:
105 // ["a" "b" "c"]
106 // ["" "man " "plan " "canal panama"]
107 // [" " "x" "y" "z" " "]
108 // [""]
111 func ExampleSplitN() {
112 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
113 z := strings.SplitN("a,b,c", ",", 0)
114 fmt.Printf("%q (nil = %v)\n", z, z == nil)
115 // Output:
116 // ["a" "b,c"]
117 // [] (nil = true)
120 func ExampleSplitAfter() {
121 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
122 // Output: ["a," "b," "c"]
125 func ExampleSplitAfterN() {
126 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
127 // Output: ["a," "b,c"]
130 func ExampleTitle() {
131 fmt.Println(strings.Title("her royal highness"))
132 // Output: Her Royal Highness
135 func ExampleToTitle() {
136 fmt.Println(strings.ToTitle("loud noises"))
137 fmt.Println(strings.ToTitle("хлеб"))
138 // Output:
139 // LOUD NOISES
140 // ХЛЕБ
143 func ExampleTrim() {
144 fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
145 // Output: ["Achtung"]
148 func ExampleMap() {
149 rot13 := func(r rune) rune {
150 switch {
151 case r >= 'A' && r <= 'Z':
152 return 'A' + (r-'A'+13)%26
153 case r >= 'a' && r <= 'z':
154 return 'a' + (r-'a'+13)%26
156 return r
158 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
159 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
162 func ExampleTrimSpace() {
163 fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
164 // Output: a lone gopher
167 func ExampleNewReplacer() {
168 r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
169 fmt.Println(r.Replace("This is <b>HTML</b>!"))
170 // Output: This is &lt;b&gt;HTML&lt;/b&gt;!
173 func ExampleToUpper() {
174 fmt.Println(strings.ToUpper("Gopher"))
175 // Output: GOPHER
178 func ExampleToLower() {
179 fmt.Println(strings.ToLower("Gopher"))
180 // Output: gopher