example_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. package strings_test
  5. import (
  6. "fmt"
  7. "strings"
  8. "unicode"
  9. )
  10. func ExampleFields() {
  11. fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
  12. // Output: Fields are: ["foo" "bar" "baz"]
  13. }
  14. func ExampleFieldsFunc() {
  15. f := func(c rune) bool {
  16. return !unicode.IsLetter(c) && !unicode.IsNumber(c)
  17. }
  18. fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))
  19. // Output: Fields are: ["foo1" "bar2" "baz3"]
  20. }
  21. func ExampleContains() {
  22. fmt.Println(strings.Contains("seafood", "foo"))
  23. fmt.Println(strings.Contains("seafood", "bar"))
  24. fmt.Println(strings.Contains("seafood", ""))
  25. fmt.Println(strings.Contains("", ""))
  26. // Output:
  27. // true
  28. // false
  29. // true
  30. // true
  31. }
  32. func ExampleContainsAny() {
  33. fmt.Println(strings.ContainsAny("team", "i"))
  34. fmt.Println(strings.ContainsAny("failure", "u & i"))
  35. fmt.Println(strings.ContainsAny("foo", ""))
  36. fmt.Println(strings.ContainsAny("", ""))
  37. // Output:
  38. // false
  39. // true
  40. // false
  41. // false
  42. }
  43. func ExampleCount() {
  44. fmt.Println(strings.Count("cheese", "e"))
  45. fmt.Println(strings.Count("five", "")) // before & after each rune
  46. // Output:
  47. // 3
  48. // 5
  49. }
  50. func ExampleEqualFold() {
  51. fmt.Println(strings.EqualFold("Go", "go"))
  52. // Output: true
  53. }
  54. func ExampleIndex() {
  55. fmt.Println(strings.Index("chicken", "ken"))
  56. fmt.Println(strings.Index("chicken", "dmr"))
  57. // Output:
  58. // 4
  59. // -1
  60. }
  61. func ExampleIndexFunc() {
  62. f := func(c rune) bool {
  63. return unicode.Is(unicode.Han, c)
  64. }
  65. fmt.Println(strings.IndexFunc("Hello, 世界", f))
  66. fmt.Println(strings.IndexFunc("Hello, world", f))
  67. // Output:
  68. // 7
  69. // -1
  70. }
  71. func ExampleIndexAny() {
  72. fmt.Println(strings.IndexAny("chicken", "aeiouy"))
  73. fmt.Println(strings.IndexAny("crwth", "aeiouy"))
  74. // Output:
  75. // 2
  76. // -1
  77. }
  78. func ExampleIndexRune() {
  79. fmt.Println(strings.IndexRune("chicken", 'k'))
  80. fmt.Println(strings.IndexRune("chicken", 'd'))
  81. // Output:
  82. // 4
  83. // -1
  84. }
  85. func ExampleLastIndex() {
  86. fmt.Println(strings.Index("go gopher", "go"))
  87. fmt.Println(strings.LastIndex("go gopher", "go"))
  88. fmt.Println(strings.LastIndex("go gopher", "rodent"))
  89. // Output:
  90. // 0
  91. // 3
  92. // -1
  93. }
  94. func ExampleJoin() {
  95. s := []string{"foo", "bar", "baz"}
  96. fmt.Println(strings.Join(s, ", "))
  97. // Output: foo, bar, baz
  98. }
  99. func ExampleRepeat() {
  100. fmt.Println("ba" + strings.Repeat("na", 2))
  101. // Output: banana
  102. }
  103. func ExampleReplace() {
  104. fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
  105. fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
  106. // Output:
  107. // oinky oinky oink
  108. // moo moo moo
  109. }
  110. func ExampleSplit() {
  111. fmt.Printf("%q\n", strings.Split("a,b,c", ","))
  112. fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
  113. fmt.Printf("%q\n", strings.Split(" xyz ", ""))
  114. fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
  115. // Output:
  116. // ["a" "b" "c"]
  117. // ["" "man " "plan " "canal panama"]
  118. // [" " "x" "y" "z" " "]
  119. // [""]
  120. }
  121. func ExampleSplitN() {
  122. fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))
  123. z := strings.SplitN("a,b,c", ",", 0)
  124. fmt.Printf("%q (nil = %v)\n", z, z == nil)
  125. // Output:
  126. // ["a" "b,c"]
  127. // [] (nil = true)
  128. }
  129. func ExampleSplitAfter() {
  130. fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))
  131. // Output: ["a," "b," "c"]
  132. }
  133. func ExampleSplitAfterN() {
  134. fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))
  135. // Output: ["a," "b,c"]
  136. }
  137. func ExampleTitle() {
  138. fmt.Println(strings.Title("her royal highness"))
  139. // Output: Her Royal Highness
  140. }
  141. func ExampleToTitle() {
  142. fmt.Println(strings.ToTitle("loud noises"))
  143. fmt.Println(strings.ToTitle("хлеб"))
  144. // Output:
  145. // LOUD NOISES
  146. // ХЛЕБ
  147. }
  148. func ExampleTrim() {
  149. fmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! "))
  150. // Output: ["Achtung! Achtung"]
  151. }
  152. func ExampleMap() {
  153. rot13 := func(r rune) rune {
  154. switch {
  155. case r >= 'A' && r <= 'Z':
  156. return 'A' + (r-'A'+13)%26
  157. case r >= 'a' && r <= 'z':
  158. return 'a' + (r-'a'+13)%26
  159. }
  160. return r
  161. }
  162. fmt.Println(strings.Map(rot13, "'Twas brillig and the slithy gopher..."))
  163. // Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
  164. }
  165. func ExampleTrimSpace() {
  166. fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n"))
  167. // Output: a lone gopher
  168. }
  169. func ExampleNewReplacer() {
  170. r := strings.NewReplacer("<", "&lt;", ">", "&gt;")
  171. fmt.Println(r.Replace("This is <b>HTML</b>!"))
  172. // Output: This is &lt;b&gt;HTML&lt;/b&gt;!
  173. }
  174. func ExampleToUpper() {
  175. fmt.Println(strings.ToUpper("Gopher"))
  176. // Output: GOPHER
  177. }
  178. func ExampleToLower() {
  179. fmt.Println(strings.ToLower("Gopher"))
  180. // Output: gopher
  181. }
  182. func ExampleTrimSuffix() {
  183. var s = "Hello, goodbye, etc!"
  184. s = strings.TrimSuffix(s, "goodbye, etc!")
  185. s = strings.TrimSuffix(s, "planet")
  186. fmt.Print(s, "world!")
  187. // Output: Hello, world!
  188. }
  189. func ExampleTrimPrefix() {
  190. var s = "Goodbye,, world!"
  191. s = strings.TrimPrefix(s, "Goodbye,")
  192. s = strings.TrimPrefix(s, "Howdy,")
  193. fmt.Print("Hello" + s)
  194. // Output: Hello, world!
  195. }