utils_str.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 utils
  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. // IsLetterUpper checks whether the given byte b is in upper case.
  25. func IsLetterUpper(b byte) bool {
  26. if b >= byte('A') && b <= byte('Z') {
  27. return true
  28. }
  29. return false
  30. }
  31. // IsLetterLower checks whether the given byte b is in lower case.
  32. func IsLetterLower(b byte) bool {
  33. if b >= byte('a') && b <= byte('z') {
  34. return true
  35. }
  36. return false
  37. }
  38. // IsLetter checks whether the given byte b is a letter.
  39. func IsLetter(b byte) bool {
  40. return IsLetterUpper(b) || IsLetterLower(b)
  41. }
  42. // IsNumeric checks whether the given string s is numeric.
  43. // Note that float string like "123.456" is also numeric.
  44. func IsNumeric(s string) bool {
  45. length := len(s)
  46. if length == 0 {
  47. return false
  48. }
  49. for i := 0; i < len(s); i++ {
  50. if s[i] == '-' && i == 0 {
  51. continue
  52. }
  53. if s[i] == '.' {
  54. if i > 0 && i < len(s)-1 {
  55. continue
  56. } else {
  57. return false
  58. }
  59. }
  60. if s[i] < '0' || s[i] > '9' {
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. // UcFirst returns a copy of the string s with the first letter mapped to its upper case.
  67. func UcFirst(s string) string {
  68. if len(s) == 0 {
  69. return s
  70. }
  71. if IsLetterLower(s[0]) {
  72. return string(s[0]-32) + s[1:]
  73. }
  74. return s
  75. }
  76. // ReplaceByMap returns a copy of `origin`,
  77. // which is replaced by a map in unordered way, case-sensitively.
  78. func ReplaceByMap(origin string, replaces map[string]string) string {
  79. for k, v := range replaces {
  80. origin = strings.Replace(origin, k, v, -1)
  81. }
  82. return origin
  83. }
  84. // RemoveSymbols removes all symbols from string and lefts only numbers and letters.
  85. func RemoveSymbols(s string) string {
  86. var b []byte
  87. for _, c := range s {
  88. if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') {
  89. b = append(b, byte(c))
  90. }
  91. }
  92. return string(b)
  93. }
  94. // EqualFoldWithoutChars checks string `s1` and `s2` equal case-insensitively,
  95. // with/without chars '-'/'_'/'.'/' '.
  96. func EqualFoldWithoutChars(s1, s2 string) bool {
  97. return strings.EqualFold(RemoveSymbols(s1), RemoveSymbols(s2))
  98. }
  99. // SplitAndTrim splits string <str> by a string <delimiter> to an array,
  100. // and calls Trim to every element of this array. It ignores the elements
  101. // which are empty after Trim.
  102. func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
  103. array := make([]string, 0)
  104. for _, v := range strings.Split(str, delimiter) {
  105. v = Trim(v, characterMask...)
  106. if v != "" {
  107. array = append(array, v)
  108. }
  109. }
  110. return array
  111. }
  112. // Trim strips whitespace (or other characters) from the beginning and end of a string.
  113. // The optional parameter <characterMask> specifies the additional stripped characters.
  114. func Trim(str string, characterMask ...string) string {
  115. trimChars := DefaultTrimChars
  116. if len(characterMask) > 0 {
  117. trimChars += characterMask[0]
  118. }
  119. return strings.Trim(str, trimChars)
  120. }