gstr_replace.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "strings"
  9. "github.com/gogf/gf/v2/internal/utils"
  10. )
  11. // Replace returns a copy of the string `origin`
  12. // in which string `search` replaced by `replace` case-sensitively.
  13. func Replace(origin, search, replace string, count ...int) string {
  14. n := -1
  15. if len(count) > 0 {
  16. n = count[0]
  17. }
  18. return strings.Replace(origin, search, replace, n)
  19. }
  20. // ReplaceI returns a copy of the string `origin`
  21. // in which string `search` replaced by `replace` case-insensitively.
  22. func ReplaceI(origin, search, replace string, count ...int) string {
  23. n := -1
  24. if len(count) > 0 {
  25. n = count[0]
  26. }
  27. if n == 0 {
  28. return origin
  29. }
  30. var (
  31. searchLength = len(search)
  32. replaceLength = len(replace)
  33. searchLower = strings.ToLower(search)
  34. originLower string
  35. pos int
  36. )
  37. for {
  38. originLower = strings.ToLower(origin)
  39. if pos = Pos(originLower, searchLower, pos); pos != -1 {
  40. origin = origin[:pos] + replace + origin[pos+searchLength:]
  41. pos += replaceLength
  42. if n--; n == 0 {
  43. break
  44. }
  45. } else {
  46. break
  47. }
  48. }
  49. return origin
  50. }
  51. // ReplaceByArray returns a copy of `origin`,
  52. // which is replaced by a slice in order, case-sensitively.
  53. func ReplaceByArray(origin string, array []string) string {
  54. for i := 0; i < len(array); i += 2 {
  55. if i+1 >= len(array) {
  56. break
  57. }
  58. origin = Replace(origin, array[i], array[i+1])
  59. }
  60. return origin
  61. }
  62. // ReplaceIByArray returns a copy of `origin`,
  63. // which is replaced by a slice in order, case-insensitively.
  64. func ReplaceIByArray(origin string, array []string) string {
  65. for i := 0; i < len(array); i += 2 {
  66. if i+1 >= len(array) {
  67. break
  68. }
  69. origin = ReplaceI(origin, array[i], array[i+1])
  70. }
  71. return origin
  72. }
  73. // ReplaceByMap returns a copy of `origin`,
  74. // which is replaced by a map in unordered way, case-sensitively.
  75. func ReplaceByMap(origin string, replaces map[string]string) string {
  76. return utils.ReplaceByMap(origin, replaces)
  77. }
  78. // ReplaceIByMap returns a copy of `origin`,
  79. // which is replaced by a map in unordered way, case-insensitively.
  80. func ReplaceIByMap(origin string, replaces map[string]string) string {
  81. for k, v := range replaces {
  82. origin = ReplaceI(origin, k, v)
  83. }
  84. return origin
  85. }