github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/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 ExampleCompare() { 27 fmt.Println(strings.Compare("a", "b")) 28 fmt.Println(strings.Compare("a", "a")) 29 fmt.Println(strings.Compare("b", "a")) 30 // Output: 31 // -1 32 // 0 33 // 1 34 } 35 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("", "")) 41 // Output: 42 // true 43 // false 44 // true 45 // true 46 } 47 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("", "")) 53 // Output: 54 // false 55 // true 56 // false 57 // false 58 } 59 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)) 65 // Output: 66 // true 67 // false 68 } 69 70 func ExampleCount() { 71 fmt.Println(strings.Count("cheese", "e")) 72 fmt.Println(strings.Count("five", "")) // before & after each rune 73 // Output: 74 // 3 75 // 5 76 } 77 78 func ExampleEqualFold() { 79 fmt.Println(strings.EqualFold("Go", "go")) 80 // Output: true 81 } 82 83 func ExampleHasPrefix() { 84 fmt.Println(strings.HasPrefix("Gopher", "Go")) 85 fmt.Println(strings.HasPrefix("Gopher", "C")) 86 fmt.Println(strings.HasPrefix("Gopher", "")) 87 // Output: 88 // true 89 // false 90 // true 91 } 92 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", "")) 98 // Output: 99 // true 100 // false 101 // false 102 // true 103 } 104 105 func ExampleIndex() { 106 fmt.Println(strings.Index("chicken", "ken")) 107 fmt.Println(strings.Index("chicken", "dmr")) 108 // Output: 109 // 4 110 // -1 111 } 112 113 func ExampleIndexFunc() { 114 f := func(c rune) bool { 115 return unicode.Is(unicode.Han, c) 116 } 117 fmt.Println(strings.IndexFunc("Hello, 世界", f)) 118 fmt.Println(strings.IndexFunc("Hello, world", f)) 119 // Output: 120 // 7 121 // -1 122 } 123 124 func ExampleIndexAny() { 125 fmt.Println(strings.IndexAny("chicken", "aeiouy")) 126 fmt.Println(strings.IndexAny("crwth", "aeiouy")) 127 // Output: 128 // 2 129 // -1 130 } 131 132 func ExampleIndexByte() { 133 fmt.Println(strings.IndexByte("golang", 'g')) 134 fmt.Println(strings.IndexByte("gophers", 'h')) 135 fmt.Println(strings.IndexByte("golang", 'x')) 136 // Output: 137 // 0 138 // 3 139 // -1 140 } 141 func ExampleIndexRune() { 142 fmt.Println(strings.IndexRune("chicken", 'k')) 143 fmt.Println(strings.IndexRune("chicken", 'd')) 144 // Output: 145 // 4 146 // -1 147 } 148 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")) 153 // Output: 154 // 0 155 // 3 156 // -1 157 } 158 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")) 163 // Output: 164 // 4 165 // 8 166 // -1 167 } 168 169 func ExampleLastIndexByte() { 170 fmt.Println(strings.LastIndexByte("Hello, world", 'l')) 171 fmt.Println(strings.LastIndexByte("Hello, world", 'o')) 172 fmt.Println(strings.LastIndexByte("Hello, world", 'x')) 173 // Output: 174 // 10 175 // 8 176 // -1 177 } 178 179 func ExampleLastIndexFunc() { 180 fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber)) 181 fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber)) 182 fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber)) 183 // Output: 184 // 5 185 // 2 186 // -1 187 } 188 189 func ExampleJoin() { 190 s := []string{"foo", "bar", "baz"} 191 fmt.Println(strings.Join(s, ", ")) 192 // Output: foo, bar, baz 193 } 194 195 func ExampleRepeat() { 196 fmt.Println("ba" + strings.Repeat("na", 2)) 197 // Output: banana 198 } 199 200 func ExampleReplace() { 201 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) 202 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) 203 // Output: 204 // oinky oinky oink 205 // moo moo moo 206 } 207 208 func ExampleSplit() { 209 fmt.Printf("%q\n", strings.Split("a,b,c", ",")) 210 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) 211 fmt.Printf("%q\n", strings.Split(" xyz ", "")) 212 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) 213 // Output: 214 // ["a" "b" "c"] 215 // ["" "man " "plan " "canal panama"] 216 // [" " "x" "y" "z" " "] 217 // [""] 218 } 219 220 func ExampleSplitN() { 221 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) 222 z := strings.SplitN("a,b,c", ",", 0) 223 fmt.Printf("%q (nil = %v)\n", z, z == nil) 224 // Output: 225 // ["a" "b,c"] 226 // [] (nil = true) 227 } 228 229 func ExampleSplitAfter() { 230 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) 231 // Output: ["a," "b," "c"] 232 } 233 234 func ExampleSplitAfterN() { 235 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) 236 // Output: ["a," "b,c"] 237 } 238 239 func ExampleTitle() { 240 fmt.Println(strings.Title("her royal highness")) 241 // Output: Her Royal Highness 242 } 243 244 func ExampleToTitle() { 245 fmt.Println(strings.ToTitle("loud noises")) 246 fmt.Println(strings.ToTitle("хлеб")) 247 // Output: 248 // LOUD NOISES 249 // ХЛЕБ 250 } 251 252 func ExampleToTitleSpecial() { 253 fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir")) 254 // Output: 255 // DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR 256 } 257 258 func ExampleMap() { 259 rot13 := func(r rune) rune { 260 switch { 261 case r >= 'A' && r <= 'Z': 262 return 'A' + (r-'A'+13)%26 263 case r >= 'a' && r <= 'z': 264 return 'a' + (r-'a'+13)%26 265 } 266 return r 267 } 268 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) 269 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 270 } 271 272 func ExampleNewReplacer() { 273 r := strings.NewReplacer("<", "<", ">", ">") 274 fmt.Println(r.Replace("This is <b>HTML</b>!")) 275 // Output: This is <b>HTML</b>! 276 } 277 278 func ExampleToUpper() { 279 fmt.Println(strings.ToUpper("Gopher")) 280 // Output: GOPHER 281 } 282 283 func ExampleToUpperSpecial() { 284 fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş")) 285 // Output: ÖRNEK İŞ 286 } 287 288 func ExampleToLower() { 289 fmt.Println(strings.ToLower("Gopher")) 290 // Output: gopher 291 } 292 293 func ExampleToLowerSpecial() { 294 fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş")) 295 // Output: önnek iş 296 } 297 298 func ExampleTrim() { 299 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) 300 // Output: Hello, Gophers 301 } 302 303 func ExampleTrimSpace() { 304 fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) 305 // Output: Hello, Gophers 306 } 307 308 func ExampleTrimPrefix() { 309 var s = "¡¡¡Hello, Gophers!!!" 310 s = strings.TrimPrefix(s, "¡¡¡Hello, ") 311 s = strings.TrimPrefix(s, "¡¡¡Howdy, ") 312 fmt.Print(s) 313 // Output: Gophers!!! 314 } 315 316 func ExampleTrimSuffix() { 317 var s = "¡¡¡Hello, Gophers!!!" 318 s = strings.TrimSuffix(s, ", Gophers!!!") 319 s = strings.TrimSuffix(s, ", Marmots!!!") 320 fmt.Print(s) 321 // Output: ¡¡¡Hello 322 } 323 324 func ExampleTrimFunc() { 325 fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 326 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 327 })) 328 // Output: Hello, Gophers 329 } 330 331 func ExampleTrimLeft() { 332 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) 333 // Output: Hello, Gophers!!! 334 } 335 336 func ExampleTrimLeftFunc() { 337 fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 338 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 339 })) 340 // Output: Hello, Gophers!!! 341 } 342 343 func ExampleTrimRight() { 344 fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡")) 345 // Output: ¡¡¡Hello, Gophers 346 } 347 348 func ExampleTrimRightFunc() { 349 fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 350 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 351 })) 352 // Output: ¡¡¡Hello, Gophers 353 } 354 355 func ExampleBuilder() { 356 var b strings.Builder 357 for i := 3; i >= 1; i-- { 358 fmt.Fprintf(&b, "%d...", i) 359 } 360 b.WriteString("ignition") 361 fmt.Println(b.String()) 362 363 // Output: 3...2...1...ignition 364 }