github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/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 ExampleReplaceAll() { 209 fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo")) 210 // Output: 211 // moo moo moo 212 } 213 214 func ExampleSplit() { 215 fmt.Printf("%q\n", strings.Split("a,b,c", ",")) 216 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) 217 fmt.Printf("%q\n", strings.Split(" xyz ", "")) 218 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) 219 // Output: 220 // ["a" "b" "c"] 221 // ["" "man " "plan " "canal panama"] 222 // [" " "x" "y" "z" " "] 223 // [""] 224 } 225 226 func ExampleSplitN() { 227 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) 228 z := strings.SplitN("a,b,c", ",", 0) 229 fmt.Printf("%q (nil = %v)\n", z, z == nil) 230 // Output: 231 // ["a" "b,c"] 232 // [] (nil = true) 233 } 234 235 func ExampleSplitAfter() { 236 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) 237 // Output: ["a," "b," "c"] 238 } 239 240 func ExampleSplitAfterN() { 241 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) 242 // Output: ["a," "b,c"] 243 } 244 245 func ExampleTitle() { 246 fmt.Println(strings.Title("her royal highness")) 247 // Output: Her Royal Highness 248 } 249 250 func ExampleToTitle() { 251 fmt.Println(strings.ToTitle("loud noises")) 252 fmt.Println(strings.ToTitle("хлеб")) 253 // Output: 254 // LOUD NOISES 255 // ХЛЕБ 256 } 257 258 func ExampleToTitleSpecial() { 259 fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir")) 260 // Output: 261 // DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR 262 } 263 264 func ExampleMap() { 265 rot13 := func(r rune) rune { 266 switch { 267 case r >= 'A' && r <= 'Z': 268 return 'A' + (r-'A'+13)%26 269 case r >= 'a' && r <= 'z': 270 return 'a' + (r-'a'+13)%26 271 } 272 return r 273 } 274 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) 275 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 276 } 277 278 func ExampleNewReplacer() { 279 r := strings.NewReplacer("<", "<", ">", ">") 280 fmt.Println(r.Replace("This is <b>HTML</b>!")) 281 // Output: This is <b>HTML</b>! 282 } 283 284 func ExampleToUpper() { 285 fmt.Println(strings.ToUpper("Gopher")) 286 // Output: GOPHER 287 } 288 289 func ExampleToUpperSpecial() { 290 fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş")) 291 // Output: ÖRNEK İŞ 292 } 293 294 func ExampleToLower() { 295 fmt.Println(strings.ToLower("Gopher")) 296 // Output: gopher 297 } 298 299 func ExampleToLowerSpecial() { 300 fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş")) 301 // Output: önnek iş 302 } 303 304 func ExampleTrim() { 305 fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) 306 // Output: Hello, Gophers 307 } 308 309 func ExampleTrimSpace() { 310 fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) 311 // Output: Hello, Gophers 312 } 313 314 func ExampleTrimPrefix() { 315 var s = "¡¡¡Hello, Gophers!!!" 316 s = strings.TrimPrefix(s, "¡¡¡Hello, ") 317 s = strings.TrimPrefix(s, "¡¡¡Howdy, ") 318 fmt.Print(s) 319 // Output: Gophers!!! 320 } 321 322 func ExampleTrimSuffix() { 323 var s = "¡¡¡Hello, Gophers!!!" 324 s = strings.TrimSuffix(s, ", Gophers!!!") 325 s = strings.TrimSuffix(s, ", Marmots!!!") 326 fmt.Print(s) 327 // Output: ¡¡¡Hello 328 } 329 330 func ExampleTrimFunc() { 331 fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 332 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 333 })) 334 // Output: Hello, Gophers 335 } 336 337 func ExampleTrimLeft() { 338 fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) 339 // Output: Hello, Gophers!!! 340 } 341 342 func ExampleTrimLeftFunc() { 343 fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 344 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 345 })) 346 // Output: Hello, Gophers!!! 347 } 348 349 func ExampleTrimRight() { 350 fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡")) 351 // Output: ¡¡¡Hello, Gophers 352 } 353 354 func ExampleTrimRightFunc() { 355 fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { 356 return !unicode.IsLetter(r) && !unicode.IsNumber(r) 357 })) 358 // Output: ¡¡¡Hello, Gophers 359 } 360 361 func ExampleBuilder() { 362 var b strings.Builder 363 for i := 3; i >= 1; i-- { 364 fmt.Fprintf(&b, "%d...", i) 365 } 366 b.WriteString("ignition") 367 fmt.Println(b.String()) 368 369 // Output: 3...2...1...ignition 370 }