gstr_sub.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. func Str(haystack string, needle string) string {
  12. if needle == "" {
  13. return ""
  14. }
  15. pos := strings.Index(haystack, needle)
  16. if pos == NotFoundIndex {
  17. return ""
  18. }
  19. return haystack[pos+len([]byte(needle))-1:]
  20. }
  21. // StrEx returns part of `haystack` string starting from and excluding
  22. // the first occurrence of `needle` to the end of `haystack`.
  23. func StrEx(haystack string, needle string) string {
  24. if s := Str(haystack, needle); s != "" {
  25. return s[1:]
  26. }
  27. return ""
  28. }
  29. // StrTill returns part of `haystack` string ending to and including
  30. // the first occurrence of `needle` from the start of `haystack`.
  31. func StrTill(haystack string, needle string) string {
  32. pos := strings.Index(haystack, needle)
  33. if pos == NotFoundIndex || pos == 0 {
  34. return ""
  35. }
  36. return haystack[:pos+1]
  37. }
  38. // StrTillEx returns part of `haystack` string ending to and excluding
  39. // the first occurrence of `needle` from the start of `haystack`.
  40. func StrTillEx(haystack string, needle string) string {
  41. pos := strings.Index(haystack, needle)
  42. if pos == NotFoundIndex || pos == 0 {
  43. return ""
  44. }
  45. return haystack[:pos]
  46. }
  47. // SubStr returns a portion of string `str` specified by the `start` and `length` parameters.
  48. // The parameter `length` is optional, it uses the length of `str` in default.
  49. func SubStr(str string, start int, length ...int) (substr string) {
  50. strLength := len(str)
  51. // Simple border checks.
  52. if start < 0 {
  53. start = 0
  54. }
  55. if start >= strLength {
  56. start = strLength
  57. }
  58. end := strLength
  59. if len(length) > 0 {
  60. end = start + length[0]
  61. if end < start {
  62. end = strLength
  63. }
  64. }
  65. if end > strLength {
  66. end = strLength
  67. }
  68. return str[start:end]
  69. }
  70. // SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters.
  71. // SubStrRune considers parameter `str` as unicode string.
  72. // The parameter `length` is optional, it uses the length of `str` in default.
  73. func SubStrRune(str string, start int, length ...int) (substr string) {
  74. // Converting to []rune to support unicode.
  75. var (
  76. runes = []rune(str)
  77. runesLength = len(runes)
  78. )
  79. // Simple border checks.
  80. if start < 0 {
  81. start = 0
  82. }
  83. if start >= runesLength {
  84. start = runesLength
  85. }
  86. end := runesLength
  87. if len(length) > 0 {
  88. end = start + length[0]
  89. if end < start {
  90. end = runesLength
  91. }
  92. }
  93. if end > runesLength {
  94. end = runesLength
  95. }
  96. return string(runes[start:end])
  97. }
  98. // StrLimit returns a portion of string `str` specified by `length` parameters, if the length
  99. // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
  100. func StrLimit(str string, length int, suffix ...string) string {
  101. if len(str) < length {
  102. return str
  103. }
  104. suffixStr := defaultSuffixForStrLimit
  105. if len(suffix) > 0 {
  106. suffixStr = suffix[0]
  107. }
  108. return str[0:length] + suffixStr
  109. }
  110. // StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length
  111. // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
  112. // StrLimitRune considers parameter `str` as unicode string.
  113. func StrLimitRune(str string, length int, suffix ...string) string {
  114. runes := []rune(str)
  115. if len(runes) < length {
  116. return str
  117. }
  118. suffixStr := defaultSuffixForStrLimit
  119. if len(suffix) > 0 {
  120. suffixStr = suffix[0]
  121. }
  122. return string(runes[0:length]) + suffixStr
  123. }
  124. // SubStrFrom returns a portion of string `str` starting from first occurrence of and including `need`
  125. // to the end of `str`.
  126. func SubStrFrom(str string, need string) (substr string) {
  127. pos := Pos(str, need)
  128. if pos < 0 {
  129. return ""
  130. }
  131. return str[pos:]
  132. }
  133. // SubStrFromEx returns a portion of string `str` starting from first occurrence of and excluding `need`
  134. // to the end of `str`.
  135. func SubStrFromEx(str string, need string) (substr string) {
  136. pos := Pos(str, need)
  137. if pos < 0 {
  138. return ""
  139. }
  140. return str[pos+len(need):]
  141. }
  142. // SubStrFromR returns a portion of string `str` starting from last occurrence of and including `need`
  143. // to the end of `str`.
  144. func SubStrFromR(str string, need string) (substr string) {
  145. pos := PosR(str, need)
  146. if pos < 0 {
  147. return ""
  148. }
  149. return str[pos:]
  150. }
  151. // SubStrFromREx returns a portion of string `str` starting from last occurrence of and excluding `need`
  152. // to the end of `str`.
  153. func SubStrFromREx(str string, need string) (substr string) {
  154. pos := PosR(str, need)
  155. if pos < 0 {
  156. return ""
  157. }
  158. return str[pos+len(need):]
  159. }