gstr_trim.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 (
  8. "github.com/gogf/gf/internal/utils"
  9. "strings"
  10. )
  11. // Trim strips whitespace (or other characters) from the beginning and end of a string.
  12. // The optional parameter <characterMask> specifies the additional stripped characters.
  13. func Trim(str string, characterMask ...string) string {
  14. return utils.Trim(str, characterMask...)
  15. }
  16. // TrimStr strips all the given <cut> string from the beginning and end of a string.
  17. // Note that it does not strip the whitespaces of its beginning or end.
  18. func TrimStr(str string, cut string, count ...int) string {
  19. return TrimLeftStr(TrimRightStr(str, cut, count...), cut, count...)
  20. }
  21. // TrimLeft strips whitespace (or other characters) from the beginning of a string.
  22. func TrimLeft(str string, characterMask ...string) string {
  23. trimChars := utils.DefaultTrimChars
  24. if len(characterMask) > 0 {
  25. trimChars += characterMask[0]
  26. }
  27. return strings.TrimLeft(str, trimChars)
  28. }
  29. // TrimLeftStr strips all the given <cut> string from the beginning of a string.
  30. // Note that it does not strip the whitespaces of its beginning.
  31. func TrimLeftStr(str string, cut string, count ...int) string {
  32. var (
  33. lenCut = len(cut)
  34. cutCount = 0
  35. )
  36. for len(str) >= lenCut && str[0:lenCut] == cut {
  37. str = str[lenCut:]
  38. cutCount++
  39. if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
  40. break
  41. }
  42. }
  43. return str
  44. }
  45. // TrimRight strips whitespace (or other characters) from the end of a string.
  46. func TrimRight(str string, characterMask ...string) string {
  47. trimChars := utils.DefaultTrimChars
  48. if len(characterMask) > 0 {
  49. trimChars += characterMask[0]
  50. }
  51. return strings.TrimRight(str, trimChars)
  52. }
  53. // TrimRightStr strips all the given <cut> string from the end of a string.
  54. // Note that it does not strip the whitespaces of its end.
  55. func TrimRightStr(str string, cut string, count ...int) string {
  56. var (
  57. lenStr = len(str)
  58. lenCut = len(cut)
  59. cutCount = 0
  60. )
  61. for lenStr >= lenCut && str[lenStr-lenCut:lenStr] == cut {
  62. lenStr = lenStr - lenCut
  63. str = str[:lenStr]
  64. cutCount++
  65. if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
  66. break
  67. }
  68. }
  69. return str
  70. }
  71. // TrimAll trims all characters in string `str`.
  72. func TrimAll(str string, characterMask ...string) string {
  73. trimChars := utils.DefaultTrimChars
  74. if len(characterMask) > 0 {
  75. trimChars += characterMask[0]
  76. }
  77. var (
  78. filtered bool
  79. slice = make([]rune, 0, len(str))
  80. )
  81. for _, char := range str {
  82. filtered = false
  83. for _, trimChar := range trimChars {
  84. if char == trimChar {
  85. filtered = true
  86. break
  87. }
  88. }
  89. if !filtered {
  90. slice = append(slice, char)
  91. }
  92. }
  93. return string(slice)
  94. }