Guest User

Untitled

a guest
Aug 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. ## Finding the substring in a string
  2. ```go
  3. package main
  4.  
  5. import (
  6. "fmt"
  7. "strings"
  8. )
  9.  
  10. const refString = "Mary had a little lamb"
  11.  
  12. func main() {
  13.  
  14. lookFor := "lamb"
  15. contain := strings.Contains(refString, lookFor)
  16. fmt.Printf("The \"%s\" contains \"%s\": %t \n", refString,
  17. lookFor, contain)
  18.  
  19. lookFor = "wolf"
  20. contain = strings.Contains(refString, lookFor)
  21. fmt.Printf("The \"%s\" contains \"%s\": %t \n", refString,
  22. lookFor, contain)
  23.  
  24. startsWith := "Mary"
  25. starts := strings.HasPrefix(refString, startsWith)
  26. fmt.Printf("The \"%s\" starts with \"%s\": %t \n", refString,
  27. startsWith, starts)
  28.  
  29. endWith := "lamb"
  30. ends := strings.HasSuffix(refString, endWith)
  31. fmt.Printf("The \"%s\" ends with \"%s\": %t \n", refString,
  32. endWith, ends)
  33.  
  34. }
  35. ```
  36. ## Breaking the string into words
  37. ```go
  38. package main
  39.  
  40. import (
  41. "fmt"
  42. "strings"
  43. )
  44.  
  45. const refString = "Mary had a little lamb"
  46.  
  47. func main() {
  48.  
  49. words := strings.Fields(refString)
  50. for idx, word := range words {
  51. fmt.Printf("Word %d is: %s\n", idx, word)
  52. }
  53.  
  54. }
  55. ```
  56. ```go
  57. package main
  58.  
  59. import (
  60. "fmt"
  61. "strings"
  62. )
  63.  
  64. const refString = "Mary_had a little_lamb"
  65.  
  66. func main() {
  67.  
  68. words := strings.Split(refString, "_")
  69. for idx, word := range words {
  70. fmt.Printf("Word %d is: %s\n", idx, word)
  71. }
  72.  
  73. }
  74. ```
  75. ```go
  76. package main
  77.  
  78. import (
  79. "fmt"
  80. "strings"
  81. )
  82.  
  83. const refString = "Mary*had,a%little_lamb"
  84.  
  85. func main() {
  86.  
  87. // The splitFunc is called for each
  88. // rune in a string. If the rune
  89. // equals any of character in a "*%,_"
  90. // the refString is split.
  91. splitFunc := func(r rune) bool {
  92. return strings.ContainsRune("*%,_", r)
  93. }
  94.  
  95. words := strings.FieldsFunc(refString, splitFunc)
  96. for idx, word := range words {
  97. fmt.Printf("Word %d is: %s\n", idx, word)
  98. }
  99.  
  100. }
  101. ```
  102. ```go
  103. package main
  104.  
  105. import (
  106. "fmt"
  107. "regexp"
  108. )
  109.  
  110. const refString = "Mary*had,a%little_lamb"
  111.  
  112. func main() {
  113.  
  114. words := regexp.MustCompile("[*,%_]{1}").Split(refString, -1)
  115. for idx, word := range words {
  116. fmt.Printf("Word %d is: %s\n", idx, word)
  117. }
  118.  
  119. }
  120. ```
  121. ## Joining the string slice with a separator
  122. ```go
  123. ```
  124. ## Concatenating a string with writer
  125. ```go
  126. ```
  127. ## Aligning text with tabwriter
  128. ```go
  129. ```
  130. ## Replacing part of the string
  131. ```go
  132. ```
  133. ## Finding the substring in text by the regex pattern
  134. ```go
  135. ```
  136. ## Decoding a string from the non-Unicode charset
  137. ```go
  138. ```
  139. ## Controlling case
  140. ```go
  141. ```
  142. ## Parsing comma-separated data
  143. ```go
  144. ```
  145. ## Managing whitespace in a string
  146. ```go
  147. ```
  148. ## Indenting a text document
  149. ```go
  150. ```
Add Comment
Please, Sign In to add comment