gstr_substr.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // SubStr returns a portion of string `str` specified by the `start` and `length` parameters.
  8. // The parameter `length` is optional, it uses the length of `str` in default.
  9. func SubStr(str string, start int, length ...int) (substr string) {
  10. strLength := len(str)
  11. // Simple border checks.
  12. if start < 0 {
  13. start = 0
  14. }
  15. if start >= strLength {
  16. start = strLength
  17. }
  18. end := strLength
  19. if len(length) > 0 {
  20. end = start + length[0]
  21. if end < start {
  22. end = strLength
  23. }
  24. }
  25. if end > strLength {
  26. end = strLength
  27. }
  28. return str[start:end]
  29. }
  30. // SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters.
  31. // SubStrRune considers parameter `str` as unicode string.
  32. // The parameter `length` is optional, it uses the length of `str` in default.
  33. func SubStrRune(str string, start int, length ...int) (substr string) {
  34. // Converting to []rune to support unicode.
  35. var (
  36. runes = []rune(str)
  37. runesLength = len(runes)
  38. )
  39. // Simple border checks.
  40. if start < 0 {
  41. start = 0
  42. }
  43. if start >= runesLength {
  44. start = runesLength
  45. }
  46. end := runesLength
  47. if len(length) > 0 {
  48. end = start + length[0]
  49. if end < start {
  50. end = runesLength
  51. }
  52. }
  53. if end > runesLength {
  54. end = runesLength
  55. }
  56. return string(runes[start:end])
  57. }
  58. // StrLimit returns a portion of string `str` specified by `length` parameters, if the length
  59. // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
  60. func StrLimit(str string, length int, suffix ...string) string {
  61. if len(str) < length {
  62. return str
  63. }
  64. suffixStr := defaultSuffixForStrLimit
  65. if len(suffix) > 0 {
  66. suffixStr = suffix[0]
  67. }
  68. return str[0:length] + suffixStr
  69. }
  70. // StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length
  71. // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
  72. // StrLimitRune considers parameter `str` as unicode string.
  73. func StrLimitRune(str string, length int, suffix ...string) string {
  74. runes := []rune(str)
  75. if len(runes) < length {
  76. return str
  77. }
  78. suffixStr := defaultSuffixForStrLimit
  79. if len(suffix) > 0 {
  80. suffixStr = suffix[0]
  81. }
  82. return string(runes[0:length]) + suffixStr
  83. }