github.com/gogf/gf@v1.16.9/internal/utils/utils_str.go (about) 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 7 package utils 8 9 import ( 10 "strings" 11 ) 12 13 var ( 14 // DefaultTrimChars are the characters which are stripped by Trim* functions in default. 15 DefaultTrimChars = string([]byte{ 16 '\t', // Tab. 17 '\v', // Vertical tab. 18 '\n', // New line (line feed). 19 '\r', // Carriage return. 20 '\f', // New page. 21 ' ', // Ordinary space. 22 0x00, // NUL-byte. 23 0x85, // Delete. 24 0xA0, // Non-breaking space. 25 }) 26 ) 27 28 // IsLetterUpper checks whether the given byte b is in upper case. 29 func IsLetterUpper(b byte) bool { 30 if b >= byte('A') && b <= byte('Z') { 31 return true 32 } 33 return false 34 } 35 36 // IsLetterLower checks whether the given byte b is in lower case. 37 func IsLetterLower(b byte) bool { 38 if b >= byte('a') && b <= byte('z') { 39 return true 40 } 41 return false 42 } 43 44 // IsLetter checks whether the given byte b is a letter. 45 func IsLetter(b byte) bool { 46 return IsLetterUpper(b) || IsLetterLower(b) 47 } 48 49 // IsNumeric checks whether the given string s is numeric. 50 // Note that float string like "123.456" is also numeric. 51 func IsNumeric(s string) bool { 52 length := len(s) 53 if length == 0 { 54 return false 55 } 56 for i := 0; i < len(s); i++ { 57 if s[i] == '-' && i == 0 { 58 continue 59 } 60 if s[i] == '.' { 61 if i > 0 && i < len(s)-1 { 62 continue 63 } else { 64 return false 65 } 66 } 67 if s[i] < '0' || s[i] > '9' { 68 return false 69 } 70 } 71 return true 72 } 73 74 // UcFirst returns a copy of the string s with the first letter mapped to its upper case. 75 func UcFirst(s string) string { 76 if len(s) == 0 { 77 return s 78 } 79 if IsLetterLower(s[0]) { 80 return string(s[0]-32) + s[1:] 81 } 82 return s 83 } 84 85 // ReplaceByMap returns a copy of `origin`, 86 // which is replaced by a map in unordered way, case-sensitively. 87 func ReplaceByMap(origin string, replaces map[string]string) string { 88 for k, v := range replaces { 89 origin = strings.Replace(origin, k, v, -1) 90 } 91 return origin 92 } 93 94 // RemoveSymbols removes all symbols from string and lefts only numbers and letters. 95 func RemoveSymbols(s string) string { 96 var b []byte 97 for _, c := range s { 98 if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') { 99 b = append(b, byte(c)) 100 } 101 } 102 return string(b) 103 } 104 105 // EqualFoldWithoutChars checks string `s1` and `s2` equal case-insensitively, 106 // with/without chars '-'/'_'/'.'/' '. 107 func EqualFoldWithoutChars(s1, s2 string) bool { 108 return strings.EqualFold(RemoveSymbols(s1), RemoveSymbols(s2)) 109 } 110 111 // SplitAndTrim splits string <str> by a string <delimiter> to an array, 112 // and calls Trim to every element of this array. It ignores the elements 113 // which are empty after Trim. 114 func SplitAndTrim(str, delimiter string, characterMask ...string) []string { 115 array := make([]string, 0) 116 for _, v := range strings.Split(str, delimiter) { 117 v = Trim(v, characterMask...) 118 if v != "" { 119 array = append(array, v) 120 } 121 } 122 return array 123 } 124 125 // Trim strips whitespace (or other characters) from the beginning and end of a string. 126 // The optional parameter <characterMask> specifies the additional stripped characters. 127 func Trim(str string, characterMask ...string) string { 128 trimChars := DefaultTrimChars 129 if len(characterMask) > 0 { 130 trimChars += characterMask[0] 131 } 132 return strings.Trim(str, trimChars) 133 }