gstr_sub.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gstr
  7. import "strings"
  8. // Str returns part of `haystack` string starting from and including
  9. // the first occurrence of `needle` to the end of `haystack`.
  10. // See http://php.net/manual/en/function.strstr.php.
  11. // Eg: Str("12345", "3") => "345"
  12. func Str(haystack string, needle string) string {
  13. if needle == "" {
  14. return ""
  15. }
  16. pos := strings.Index(haystack, needle)
  17. if pos == NotFoundIndex {
  18. return ""
  19. }
  20. return haystack[pos+len([]byte(needle))-1:]
  21. }
  22. // StrEx returns part of `haystack` string starting from and excluding
  23. // the first occurrence of `needle` to the end of `haystack`.
  24. // Eg: StrEx("12345", "3") => "45"
  25. func StrEx(haystack string, needle string) string {
  26. if s := Str(haystack, needle); s != "" {
  27. return s[1:]
  28. }
  29. return ""
  30. }
  31. // StrTill returns part of `haystack` string ending to and including
  32. // the first occurrence of `needle` from the start of `haystack`.
  33. // Eg: StrTill("12345", "3") => "123"
  34. func StrTill(haystack string, needle string) string {
  35. pos := strings.Index(haystack, needle)
  36. if pos == NotFoundIndex || pos == 0 {
  37. return ""
  38. }
  39. return haystack[:pos+1]
  40. }
  41. // StrTillEx returns part of `haystack` string ending to and excluding
  42. // the first occurrence of `needle` from the start of `haystack`.
  43. // Eg: StrTillEx("12345", "3") => "12"
  44. func StrTillEx(haystack string, needle string) string {
  45. pos := strings.Index(haystack, needle)
  46. if pos == NotFoundIndex || pos == 0 {
  47. return ""
  48. }
  49. return haystack[:pos]
  50. }
  51. // SubStr returns a portion of string `str` specified by the `start` and `length` parameters.
  52. // The parameter `length` is optional, it uses the length of `str` in default.
  53. // Eg: SubStr("12345", 1, 2) => "23"
  54. func SubStr(str string, start int, length ...int) (substr string) {
  55. strLength := len(str)
  56. if start < 0 {
  57. if -start > strLength {
  58. start = 0
  59. } else {
  60. start = strLength + start
  61. }
  62. } else if start > strLength {
  63. return ""
  64. }
  65. realLength := 0
  66. if len(length) > 0 {
  67. realLength = length[0]
  68. if realLength < 0 {
  69. if -realLength > strLength-start {
  70. realLength = 0
  71. } else {
  72. realLength = strLength - start + realLength
  73. }
  74. } else if realLength > strLength-start {
  75. realLength = strLength - start
  76. }
  77. } else {
  78. realLength = strLength - start
  79. }
  80. if realLength == strLength {
  81. return str
  82. } else {
  83. end := start + realLength
  84. return str[start:end]
  85. }
  86. }
  87. // SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters.
  88. // SubStrRune considers parameter `str` as unicode string.
  89. // The parameter `length` is optional, it uses the length of `str` in default.
  90. func SubStrRune(str string, start int, length ...int) (substr string) {
  91. // Converting to []rune to support unicode.
  92. var (
  93. runes = []rune(str)
  94. runesLength = len(runes)
  95. )
  96. strLength := runesLength
  97. if start < 0 {
  98. if -start > strLength {
  99. start = 0
  100. } else {
  101. start = strLength + start
  102. }
  103. } else if start > strLength {
  104. return ""
  105. }
  106. realLength := 0
  107. if len(length) > 0 {
  108. realLength = length[0]
  109. if realLength < 0 {
  110. if -realLength > strLength-start {
  111. realLength = 0
  112. } else {
  113. realLength = strLength - start + realLength
  114. }
  115. } else if realLength > strLength-start {
  116. realLength = strLength - start
  117. }
  118. } else {
  119. realLength = strLength - start
  120. }
  121. end := start + realLength
  122. if end > runesLength {
  123. end = runesLength
  124. }
  125. return string(runes[start:end])
  126. }
  127. // StrLimit returns a portion of string `str` specified by `length` parameters, if the length
  128. // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
  129. func StrLimit(str string, length int, suffix ...string) string {
  130. if len(str) < length {
  131. return str
  132. }
  133. suffixStr := defaultSuffixForStrLimit
  134. if len(suffix) > 0 {
  135. suffixStr = suffix[0]
  136. }
  137. return str[0:length] + suffixStr
  138. }
  139. // StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length
  140. // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
  141. // StrLimitRune considers parameter `str` as unicode string.
  142. func StrLimitRune(str string, length int, suffix ...string) string {
  143. runes := []rune(str)
  144. if len(runes) < length {
  145. return str
  146. }
  147. suffixStr := defaultSuffixForStrLimit
  148. if len(suffix) > 0 {
  149. suffixStr = suffix[0]
  150. }
  151. return string(runes[0:length]) + suffixStr
  152. }
  153. // SubStrFrom returns a portion of string `str` starting from first occurrence of and including `need`
  154. // to the end of `str`.
  155. func SubStrFrom(str string, need string) (substr string) {
  156. pos := Pos(str, need)
  157. if pos < 0 {
  158. return ""
  159. }
  160. return str[pos:]
  161. }
  162. // SubStrFromEx returns a portion of string `str` starting from first occurrence of and excluding `need`
  163. // to the end of `str`.
  164. func SubStrFromEx(str string, need string) (substr string) {
  165. pos := Pos(str, need)
  166. if pos < 0 {
  167. return ""
  168. }
  169. return str[pos+len(need):]
  170. }
  171. // SubStrFromR returns a portion of string `str` starting from last occurrence of and including `need`
  172. // to the end of `str`.
  173. func SubStrFromR(str string, need string) (substr string) {
  174. pos := PosR(str, need)
  175. if pos < 0 {
  176. return ""
  177. }
  178. return str[pos:]
  179. }
  180. // SubStrFromREx returns a portion of string `str` starting from last occurrence of and excluding `need`
  181. // to the end of `str`.
  182. func SubStrFromREx(str string, need string) (substr string) {
  183. pos := PosR(str, need)
  184. if pos < 0 {
  185. return ""
  186. }
  187. return str[pos+len(need):]
  188. }