str.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package str
  2. import (
  3. "errors"
  4. "path"
  5. "strings"
  6. "unicode/utf8"
  7. )
  8. type StrUtils struct {
  9. }
  10. // ReplacePlaceholder 字符串占位符替换 占位符为"{}" String placeholder replacement The placeholder is "{}"
  11. func (u StrUtils) ReplacePlaceholder(s string, a ...interface{}) (string, error) {
  12. split := strings.Split(s, "{}")
  13. if len(split)-1 == len(a) {
  14. for _, item := range a {
  15. s = strings.Replace(s, "{}", item.(string), 1)
  16. }
  17. } else {
  18. return "", errors.New("Please check whether the number of placeholders matches the number of parameters," + s)
  19. }
  20. return s, nil
  21. }
  22. // Replace returns a copy of the string s with the first n
  23. // non-overlapping instances of old replaced by new.
  24. // If old is empty, it matches at the beginning of the string
  25. // and after each UTF-8 sequence, yielding up to k+1 replacements
  26. // for a k-rune string.
  27. // If n < 0, there is no limit on the number of replacements.
  28. func (u *StrUtils) Replace(s, old, new string, n int) string {
  29. if old == new || n == 0 {
  30. return s // avoid allocation
  31. }
  32. // Compute number of replacements.
  33. if m := strings.Count(s, old); m == 0 {
  34. return s // avoid allocation
  35. } else if n < 0 || m < n {
  36. n = m
  37. }
  38. // Apply replacements to buffer.
  39. var b strings.Builder
  40. b.Grow(len(s) + n*(len(new)-len(old)))
  41. start := 0
  42. for i := 0; i < n; i++ {
  43. j := start
  44. if len(old) == 0 {
  45. if i > 0 {
  46. _, wid := utf8.DecodeRuneInString(s[start:])
  47. j += wid
  48. }
  49. } else {
  50. j += strings.Index(s[start:], old)
  51. }
  52. b.WriteString(s[start:j])
  53. b.WriteString(new)
  54. start = j + len(old)
  55. }
  56. b.WriteString(s[start:])
  57. return b.String()
  58. }
  59. // HasEmpty It is to give some strings and return true if there are empty ones, which is often used to judge whether many fields are empty
  60. func (*StrUtils) HasEmpty(s string) bool {
  61. if s == "" || len(s) == 0 {
  62. return true
  63. }
  64. return false
  65. }
  66. func (*StrUtils) HasNotEmpty(s string) bool {
  67. if s == "" || len(s) == 0 {
  68. return false
  69. }
  70. return true
  71. }
  72. // RemoveSuffix 去掉文件扩展名,直接获取文件名称
  73. //Remove the file extension and get the file name directly
  74. func (u *StrUtils) RemoveSuffix(str string) (string, error) {
  75. if u.HasEmpty(str) {
  76. return "", errors.New("Parameter is an empty string")
  77. }
  78. filenameWithSuffix := path.Base(str)
  79. fileSuffix := path.Ext(filenameWithSuffix)
  80. filenameOnly := strings.TrimSuffix(filenameWithSuffix, fileSuffix)
  81. return filenameOnly, nil
  82. }
  83. // GetSuffix 获取文件扩展名
  84. // Get file extension
  85. func (u *StrUtils) GetSuffix(str string) (string, error) {
  86. if u.HasEmpty(str) {
  87. return "", errors.New("Parameter is an empty string")
  88. }
  89. filenameWithSuffix := path.Base(str)
  90. fileSuffix := path.Ext(filenameWithSuffix)
  91. return fileSuffix, nil
  92. }