gstr_trim.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). 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. "strings"
  9. )
  10. var (
  11. // defaultTrimChars are the characters which are stripped by Trim* functions in default.
  12. defaultTrimChars = string([]byte{
  13. '\t', // Tab.
  14. '\v', // Vertical tab.
  15. '\n', // New line (line feed).
  16. '\r', // Carriage return.
  17. '\f', // New page.
  18. ' ', // Ordinary space.
  19. 0x00, // NUL-byte.
  20. 0x85, // Delete.
  21. 0xA0, // Non-breaking space.
  22. })
  23. )
  24. // Trim strips whitespace (or other characters) from the beginning and end of a string.
  25. // The optional parameter <characterMask> specifies the additional stripped characters.
  26. func Trim(str string, characterMask ...string) string {
  27. if len(characterMask) == 0 {
  28. return strings.Trim(str, defaultTrimChars)
  29. } else {
  30. return strings.Trim(str, defaultTrimChars+characterMask[0])
  31. }
  32. }
  33. // TrimStr strips all of the given <cut> string from the beginning and end of a string.
  34. // Note that it does not strips the whitespaces of its beginning or end.
  35. func TrimStr(str string, cut string, count ...int) string {
  36. return TrimLeftStr(TrimRightStr(str, cut, count...), cut, count...)
  37. }
  38. // TrimLeft strips whitespace (or other characters) from the beginning of a string.
  39. func TrimLeft(str string, characterMask ...string) string {
  40. if len(characterMask) == 0 {
  41. return strings.TrimLeft(str, defaultTrimChars)
  42. } else {
  43. return strings.TrimLeft(str, defaultTrimChars+characterMask[0])
  44. }
  45. }
  46. // TrimLeftStr strips all of the given <cut> string from the beginning of a string.
  47. // Note that it does not strips the whitespaces of its beginning.
  48. func TrimLeftStr(str string, cut string, count ...int) string {
  49. var (
  50. lenCut = len(cut)
  51. cutCount = 0
  52. )
  53. for len(str) >= lenCut && str[0:lenCut] == cut {
  54. str = str[lenCut:]
  55. cutCount++
  56. if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
  57. break
  58. }
  59. }
  60. return str
  61. }
  62. // TrimRight strips whitespace (or other characters) from the end of a string.
  63. func TrimRight(str string, characterMask ...string) string {
  64. if len(characterMask) == 0 {
  65. return strings.TrimRight(str, defaultTrimChars)
  66. } else {
  67. return strings.TrimRight(str, defaultTrimChars+characterMask[0])
  68. }
  69. }
  70. // TrimRightStr strips all of the given <cut> string from the end of a string.
  71. // Note that it does not strips the whitespaces of its end.
  72. func TrimRightStr(str string, cut string, count ...int) string {
  73. var (
  74. lenStr = len(str)
  75. lenCut = len(cut)
  76. cutCount = 0
  77. )
  78. for lenStr >= lenCut && str[lenStr-lenCut:lenStr] == cut {
  79. lenStr = lenStr - lenCut
  80. str = str[:lenStr]
  81. cutCount++
  82. if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
  83. break
  84. }
  85. }
  86. return str
  87. }