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