github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/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 ExampleBuilder() { 14 var b strings.Builder 15 for i := 3; i >= 1; i-- { 16 fmt.Fprintf(&b, "%d...", i) 17 } 18 b.WriteString("ignition") 19 fmt.Println(b.String()) 20 21 // Output: 3...2...1...ignition 22 } 23 24 func ExampleCompare() { 25 fmt.Println(strings.Compare("a", "b")) 26 fmt.Println(strings.Compare("a", "a")) 27 fmt.Println(strings.Compare("b", "a")) 28 // Output: 29 // -1 30 // 0 31 // 1 32 } 33 34 func ExampleContains() { 35 fmt.Println(strings.Contains("seafood", "foo")) 36 fmt.Println(strings.Contains("seafood", "bar")) 37 fmt.Println(strings.Contains("seafood", "")) 38 fmt.Println(strings.Contains("", "")) 39 // Output: 40 // true 41 // false 42 // true 43 // true 44 } 45 46 func ExampleContainsAny() { 47 fmt.Println(strings.ContainsAny("team", "i")) 48 fmt.Println(strings.ContainsAny("fail", "ui")) 49 fmt.Println(strings.ContainsAny("ure", "ui")) 50 fmt.Println(strings.ContainsAny("failure", "ui")) 51 fmt.Println(strings.ContainsAny("foo", "")) 52 fmt.Println(strings.ContainsAny("", "")) 53 // Output: 54 // false 55 // true 56 // true 57 // true 58 // false 59 // false 60 } 61 62 func ExampleContainsRune() { 63 // Finds whether a string contains a particular Unicode code point. 64 // The code point for the lowercase letter "a", for example, is 97. 65 fmt.Println(strings.ContainsRune("aardvark", 97)) 66 fmt.Println(strings.ContainsRune("timeout", 97)) 67 // Output: 68 // true 69 // false 70 } 71 72 func ExampleCount() { 73 fmt.Println(strings.Count("cheese", "e")) 74 fmt.Println(strings.Count("five", "")) // before & after each rune 75 // Output: 76 // 3 77 // 5 78 } 79 80 func ExampleCut() { 81 show := func(s, sep string) { 82 before, after, found := strings.Cut(s, sep) 83 fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found) 84 } 85 show("Gopher", "Go") 86 show("Gopher", "ph") 87 show("Gopher", "er") 88 show("Gopher", "Badger") 89 // Output: 90 // Cut("Gopher", "Go") = "", "pher", true 91 // Cut("Gopher", "ph") = "Go", "er", true 92 // Cut("Gopher", "er") = "Goph", "", true 93 // Cut("Gopher", "Badger") = "Gopher", "", false 94 } 95 96 func ExampleEqualFold() { 97 fmt.Println(strings.EqualFold("Go", "go")) 98 fmt.Println(strings.EqualFold("AB", "ab")) // true because comparison uses simple case-folding 99 fmt.Println(strings.EqualFold("ß", "ss")) // false because comparison does not use full case-folding 100 // Output: 101 // true 102 // true 103 // false 104 } 105 106 func ExampleFields() { 107 fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) 108 // Output: Fields are: ["foo" "bar" "baz"] 109 } 110 111 func ExampleFieldsFunc() { 112 f := func(c rune) bool { 113 return !unicode.IsLetter(c) && !unicode.IsNumber(c) 114 } 115 fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f)) 116 // Output: Fields are: ["foo1" "bar2" "baz3"] 117 } 118 119 func ExampleHasPrefix() { 120 fmt.Println(strings.HasPrefix("Gopher", "Go")) 121 fmt.Println(strings.HasPrefix("Gopher", "C")) 122 fmt.Println(strings.HasPrefix("Gopher", "")) 123 // Output: 124 // true 125 // false 126 // true 127 } 128 129 func ExampleHasSuffix() { 130 fmt.Println(strings.HasSuffix("Amigo", "go")) 131 fmt.Println(strings.HasSuffix("Amigo", "O")) 132 fmt.Println(strings.HasSuffix("Amigo", "Ami")) 133 fmt.Println(strings.HasSuffix("Amigo", "")) 134 // Output: 135 // true 136 // false 137 // false 138 // true 139 } 140 141 func ExampleIndex() { 142 fmt.Println(strings.Index("chicken", "ken")) 143 fmt.Println(strings.Index("chicken", "dmr")) 144 // Output: 145 // 4 146 // -1 147 } 148 149 func ExampleIndexFunc() { 150 f := func(c rune) bool { 151 return unicode.Is(unicode.Han, c) 152 } 153 fmt.Println(strings.IndexFunc("Hello, 世界", f)) 154 fmt.Println(strings.IndexFunc("Hello, world", f)) 155 // Output: 156 // 7 157 // -1 158 } 159 160 func ExampleIndexAny() { 161 fmt.Println(strings.IndexAny("chicken", "aeiouy")) 162 fmt.Println(strings.IndexAny("crwth", "aeiouy")) 163 // Output: 164 // 2 165 // -1 166 } 167 168 func ExampleIndexByte() { 169 fmt.Println(strings.IndexByte("golang", 'g')) 170 fmt.Println(strings.IndexByte("gophers", 'h')) 171 fmt.Println(strings.IndexByte("golang", 'x')) 172 // Output: 173 // 0 174 // 3 175 // -1 176 } 177 func ExampleIndexRune() { 178 fmt.Println(strings.IndexRune("chicken", 'k')) 179 fmt.Println(strings.IndexRune("chicken", 'd')) 180 // Output: 181 // 4 182 // -1 183 } 184 185 func ExampleLastIndex() { 186 fmt.Println(strings.Index("go gopher", "go")) 187 fmt.Println(strings.LastIndex("go gopher", "go")) 188 fmt.Println(strings.LastIndex("go gopher", "rodent")) 189 // Output: 190 // 0 191 // 3 192 // -1 193 } 194 195 func ExampleLastIndexAny() { 196 fmt.Println(strings.LastIndexAny("go gopher", "go")) 197 fmt.Println(strings.LastIndexAny("go gopher", "rodent")) 198 fmt.Println(strings.LastIndexAny("go gopher", "fail")) 199 // Output: 200 // 4 201 // 8 202 // -1 203 } 204 205 func ExampleLastIndexByte() { 206 fmt.Println(strings.LastIndexByte("Hello, world", 'l')) 207 fmt.Println(strings.LastIndexByte("Hello, world", 'o')) 208 fmt.Println(strings.LastIndexByte("Hello, world", 'x')) 209 // Output: 210 // 10 211 // 8 212 // -1 213 } 214 215 func ExampleLastIndexFunc() { 216 fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber)) 217 fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber)) 218 fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber)) 219 // Output: 220 // 5 221 // 2 222 // -1 223 } 224 225 func ExampleJoin() { 226 s := []string{"foo", "bar", "baz"} 227 fmt.Println(strings.Join(s, ", ")) 228 // Output: foo, bar, baz 229 } 230 231 func ExampleRepeat() { 232 fmt.Println("ba" + strings.Repeat("na", 2)) 233 // Output: banana 234 } 235 236 func ExampleReplace() { 237 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) 238 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) 239 // Output: 240 // oinky oinky oink 241 // moo moo moo 242 } 243 244 func ExampleReplaceAll() { 245 fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo")) 246 // Output: 247 // moo moo moo 248 } 249 250 func ExampleSplit() { 251 fmt.Printf("%q\n", strings.Split("a,b,c", ",")) 252 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) 253 fmt.Printf("%q\n", strings.Split(" xyz ", "")) 254 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) 255 // Output: 256 // ["a" "b" "c"] 257 // ["" "man " "plan " "canal panama"] 258 // [" " "x" "y" "z" " "] 259 // [""] 260 } 261 262 func ExampleSplitN() { 263 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) 264 z := strings.SplitN("a,b,c", ",", 0) 265 fmt.Printf("%q (nil = %v)\n", z, z == nil) 266 // Output: 267 // ["a" "b,c"] 268 // [] (nil = true) 269 } 270 271 func ExampleSplitAfter() { 272 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) 273 // Output: ["a," "b," "c"] 274 } 275 276 func ExampleSplitAfterN() { 277 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) 278 // Output: ["a," "b,c"] 279 } 280 281 func ExampleTitle() { 282 // Compare this example to the ToTitle example. 283 fmt.Println(strings.Title("her royal highness")) 284 fmt.Println(strings.Title("loud noises")) 285 fmt.Println(strings.Title("хлеб")) 286 // Output: 287 // Her Royal Highness 288 // Loud Noises 289 // Хлеб 290 } 291 292 func ExampleToTitle() { 293 // Compare this example to the Title example. 294 fmt.Println(strings.ToTitle("her royal highness")) 295 fmt.Println(strings.ToTitle("loud noises")) 296 fmt.Println(strings.ToTitle("хлеб")) 297 // Output: 298 // HER ROYAL HIGHNESS 299 // LOUD NOISES 300 // ХЛЕБ 301 } 302 303 func ExampleToTitleSpecial() { 304 fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir")) 305 // Output: 306 // DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR 307 } 308 309 func ExampleMap() { 310 rot13 := func(r rune) rune { 311 switch { 312 case r >= 'A' && r <= 'Z': 313 return 'A' + (r-'A'+13)%26 314 case r >= 'a' && r <= 'z': 315 return 'a' + (r-'a'+13)%26 316 } 317 return r 318 } 319 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) 320 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 321 } 322 323 func ExampleNewReplacer() { 324 r := strings.NewReplacer("<", "<", ">", ">") 325 fmt.Println(r.Replace("This is <b>HTML</b>!")) 326 // Output: This is <b>HTML</b>! 327 } 328 329 func ExampleToUpper() { 330 fmt.Println(strings.ToUpper("Gopher")) 331 // Output: GOPHER 332 } 333 334 func ExampleToUpperSpecial() { 335 fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş")) 336 // Output: ÖRNEK İŞ 337 } 338 339 func ExampleToLower() { 340 fmt.Println(strings.ToLower("Gopher")) 341 // Output: gopher 342 } 343 344 func ExampleToLowerSpecial() { 345 fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş")) 346 // Output: önnek iş 347 } 348 349 func ExampleTrim() { 350 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) 351 // Output: Hello, Gophers 352 } 353 354 func ExampleTrimSpace() { 355 fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) 356 // Output: Hello, Gophers 357 } 358 359 func ExampleTrimPrefix() { 360 var s = "¡¡¡Hello, Gophers!!!" 361 s = strings.TrimPrefix(s, "¡¡¡Hello, ") 362 s = strings.TrimPrefix(s, "¡¡¡Howdy, ") 363 fmt.Print(s) 364 // Output: Gophers!!! 365 } 366 367 func ExampleTrimSuffix() { 368 var s = "¡¡¡Hello, Gophers!!!" 369 s = strings.TrimSuffix(s, ", Gophers!!!") 370 s = strings.TrimSuffix(s, ", Marmots!!!") 371 fmt.Print(s) 372 // Output: ¡¡¡Hello 373 } 374 375 func ExampleTrimFunc() { 376 fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 377 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 378 })) 379 // Output: Hello, Gophers 380 } 381 382 func ExampleTrimLeft() { 383 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) 384 // Output: Hello, Gophers!!! 385 } 386 387 func ExampleTrimLeftFunc() { 388 fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 389 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 390 })) 391 // Output: Hello, Gophers!!! 392 } 393 394 func ExampleTrimRight() { 395 fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡")) 396 // Output: ¡¡¡Hello, Gophers 397 } 398 399 func ExampleTrimRightFunc() { 400 fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 401 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 402 })) 403 // Output: ¡¡¡Hello, Gophers 404 }