github.com/zxy12/golang_with_comment@v0.0.0-20190701084843-0e6b2aff5ef3/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 ExampleJoin() { 170 s := []string{"foo", "bar", "baz"} 171 fmt.Println(strings.Join(s, ", ")) 172 // Output: foo, bar, baz 173 } 174 175 func ExampleRepeat() { 176 fmt.Println("ba" + strings.Repeat("na", 2)) 177 // Output: banana 178 } 179 180 func ExampleReplace() { 181 fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) 182 fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) 183 // Output: 184 // oinky oinky oink 185 // moo moo moo 186 } 187 188 func ExampleSplit() { 189 fmt.Printf("%q\n", strings.Split("a,b,c", ",")) 190 fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) 191 fmt.Printf("%q\n", strings.Split(" xyz ", "")) 192 fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) 193 // Output: 194 // ["a" "b" "c"] 195 // ["" "man " "plan " "canal panama"] 196 // [" " "x" "y" "z" " "] 197 // [""] 198 } 199 200 func ExampleSplitN() { 201 fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) 202 z := strings.SplitN("a,b,c", ",", 0) 203 fmt.Printf("%q (nil = %v)\n", z, z == nil) 204 // Output: 205 // ["a" "b,c"] 206 // [] (nil = true) 207 } 208 209 func ExampleSplitAfter() { 210 fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) 211 // Output: ["a," "b," "c"] 212 } 213 214 func ExampleSplitAfterN() { 215 fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) 216 // Output: ["a," "b,c"] 217 } 218 219 func ExampleTitle() { 220 fmt.Println(strings.Title("her royal highness")) 221 // Output: Her Royal Highness 222 } 223 224 func ExampleToTitle() { 225 fmt.Println(strings.ToTitle("loud noises")) 226 fmt.Println(strings.ToTitle("хлеб")) 227 // Output: 228 // LOUD NOISES 229 // ХЛЕБ 230 } 231 232 func ExampleTrim() { 233 fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! ")) 234 // Output: ["Achtung! Achtung"] 235 } 236 237 func ExampleTrimFunc() { 238 f := func(c rune) bool { 239 return !unicode.IsLetter(c) && !unicode.IsNumber(c) 240 } 241 fmt.Printf("[%q]", strings.TrimFunc(" Achtung1! Achtung2,...", f)) 242 // Output: ["Achtung1! Achtung2"] 243 } 244 245 func ExampleMap() { 246 rot13 := func(r rune) rune { 247 switch { 248 case r >= 'A' && r <= 'Z': 249 return 'A' + (r-'A'+13)%26 250 case r >= 'a' && r <= 'z': 251 return 'a' + (r-'a'+13)%26 252 } 253 return r 254 } 255 fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher...")) 256 // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure... 257 } 258 259 func ExampleTrimSpace() { 260 fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) 261 // Output: a lone gopher 262 } 263 264 func ExampleNewReplacer() { 265 r := strings.NewReplacer("<", "<", ">", ">") 266 fmt.Println(r.Replace("This is <b>HTML</b>!")) 267 // Output: This is <b>HTML</b>! 268 } 269 270 func ExampleToUpper() { 271 fmt.Println(strings.ToUpper("Gopher")) 272 // Output: GOPHER 273 } 274 275 func ExampleToLower() { 276 fmt.Println(strings.ToLower("Gopher")) 277 // Output: gopher 278 } 279 280 func ExampleTrimSuffix() { 281 var s = "Hello, goodbye, etc!" 282 s = strings.TrimSuffix(s, "goodbye, etc!") 283 s = strings.TrimSuffix(s, "planet") 284 fmt.Print(s, "world!") 285 // Output: Hello, world! 286 } 287 288 func ExampleTrimPrefix() { 289 var s = "Goodbye,, world!" 290 s = strings.TrimPrefix(s, "Goodbye,") 291 s = strings.TrimPrefix(s, "Howdy,") 292 fmt.Print("Hello" + s) 293 // Output: Hello, world! 294 }