github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/strings/example_test.go (about) 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 "unicode" 11 ) 12 13 func ExampleFields() { 14 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) 15 // Output: Fields are: ["foo" "bar" "baz"] 16 } 17 18 func 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 26 func 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("", "")) 31 // Output: 32 // true 33 // false 34 // true 35 // true 36 } 37 38 func 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("", "")) 43 // Output: 44 // false 45 // true 46 // false 47 // false 48 } 49 50 func ExampleCount() { 51 fmt.Println(strings.Count("cheese", "e")) 52 fmt.Println(strings.Count("five", "")) // before & after each rune 53 // Output: 54 // 3 55 // 5 56 } 57 58 func ExampleEqualFold() { 59 fmt.Println(strings.EqualFold("Go", "go")) 60 // Output: true 61 } 62 63 func ExampleHasPrefix() { 64 fmt.Println(strings.HasPrefix("Gopher", "Go")) 65 fmt.Println(strings.HasPrefix("Gopher", "C")) 66 fmt.Println(strings.HasPrefix("Gopher", "")) 67 // Output: 68 // true 69 // false 70 // true 71 } 72 73 func ExampleHasSuffix() { 74 fmt.Println(strings.HasSuffix("Amigo", "go")) 75 fmt.Println(strings.HasSuffix("Amigo", "O")) 76 fmt.Println(strings.HasSuffix("Amigo", "Ami")) 77 fmt.Println(strings.HasSuffix("Amigo", "")) 78 // Output: 79 // true 80 // false 81 // false 82 // true 83 } 84 85 func ExampleIndex() { 86 fmt.Println(strings.Index("chicken", "ken")) 87 fmt.Println(strings.Index("chicken", "dmr")) 88 // Output: 89 // 4 90 // -1 91 } 92 93 func ExampleIndexFunc() { 94 f := func(c rune) bool { 95 return unicode.Is(unicode.Han, c) 96 } 97 fmt.Println(strings.IndexFunc("Hello, 世界", f)) 98 fmt.Println(strings.IndexFunc("Hello, world", f)) 99 // Output: 100 // 7 101 // -1 102 } 103 104 func ExampleIndexAny() { 105 fmt.Println(strings.IndexAny("chicken", "aeiouy")) 106 fmt.Println(strings.IndexAny("crwth", "aeiouy")) 107 // Output: 108 // 2 109 // -1 110 } 111 112 func ExampleIndexRune() { 113 fmt.Println(strings.IndexRune("chicken", 'k')) 114 fmt.Println(strings.IndexRune("chicken", 'd')) 115 // Output: 116 // 4 117 // -1 118 } 119 120 func ExampleLastIndex() { 121 fmt.Println(strings.Index("go gopher", "go")) 122 fmt.Println(strings.LastIndex("go gopher", "go")) 123 fmt.Println(strings.LastIndex("go gopher", "rodent")) 124 // Output: 125 // 0 126 // 3 127 // -1 128 } 129 130 func ExampleJoin() { 131 s := []string{"foo", "bar", "baz"} 132 fmt.Println(strings.Join(s, ", ")) 133 // Output: foo, bar, baz 134 } 135 136 func ExampleRepeat() { 137 fmt.Println("ba" + strings.Repeat("na", 2)) 138 // Output: banana 139 } 140 141 func ExampleReplace() { 142 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) 143 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) 144 // Output: 145 // oinky oinky oink 146 // moo moo moo 147 } 148 149 func ExampleSplit() { 150 fmt.Printf("%q\n", strings.Split("a,b,c", ",")) 151 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) 152 fmt.Printf("%q\n", strings.Split(" xyz ", "")) 153 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) 154 // Output: 155 // ["a" "b" "c"] 156 // ["" "man " "plan " "canal panama"] 157 // [" " "x" "y" "z" " "] 158 // [""] 159 } 160 161 func ExampleSplitN() { 162 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) 163 z := strings.SplitN("a,b,c", ",", 0) 164 fmt.Printf("%q (nil = %v)\n", z, z == nil) 165 // Output: 166 // ["a" "b,c"] 167 // [] (nil = true) 168 } 169 170 func ExampleSplitAfter() { 171 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) 172 // Output: ["a," "b," "c"] 173 } 174 175 func ExampleSplitAfterN() { 176 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) 177 // Output: ["a," "b,c"] 178 } 179 180 func ExampleTitle() { 181 fmt.Println(strings.Title("her royal highness")) 182 // Output: Her Royal Highness 183 } 184 185 func ExampleToTitle() { 186 fmt.Println(strings.ToTitle("loud noises")) 187 fmt.Println(strings.ToTitle("хлеб")) 188 // Output: 189 // LOUD NOISES 190 // ХЛЕБ 191 } 192 193 func ExampleTrim() { 194 fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! ")) 195 // Output: ["Achtung! Achtung"] 196 } 197 198 func ExampleMap() { 199 rot13 := func(r rune) rune { 200 switch { 201 case r >= 'A' && r <= 'Z': 202 return 'A' + (r-'A'+13)%26 203 case r >= 'a' && r <= 'z': 204 return 'a' + (r-'a'+13)%26 205 } 206 return r 207 } 208 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) 209 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 210 } 211 212 func ExampleTrimSpace() { 213 fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) 214 // Output: a lone gopher 215 } 216 217 func ExampleNewReplacer() { 218 r := strings.NewReplacer("<", "<", ">", ">") 219 fmt.Println(r.Replace("This is <b>HTML</b>!")) 220 // Output: This is <b>HTML</b>! 221 } 222 223 func ExampleToUpper() { 224 fmt.Println(strings.ToUpper("Gopher")) 225 // Output: GOPHER 226 } 227 228 func ExampleToLower() { 229 fmt.Println(strings.ToLower("Gopher")) 230 // Output: gopher 231 } 232 233 func ExampleTrimSuffix() { 234 var s = "Hello, goodbye, etc!" 235 s = strings.TrimSuffix(s, "goodbye, etc!") 236 s = strings.TrimSuffix(s, "planet") 237 fmt.Print(s, "world!") 238 // Output: Hello, world! 239 } 240 241 func ExampleTrimPrefix() { 242 var s = "Goodbye,, world!" 243 s = strings.TrimPrefix(s, "Goodbye,") 244 s = strings.TrimPrefix(s, "Howdy,") 245 fmt.Print("Hello" + s) 246 // Output: Hello, world! 247 }