github.com/gogf/gf@v1.16.9/text/gstr/gstr_trim.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 gstr 8 9 import ( 10 "github.com/gogf/gf/internal/utils" 11 "strings" 12 ) 13 14 // Trim strips whitespace (or other characters) from the beginning and end of a string. 15 // The optional parameter <characterMask> specifies the additional stripped characters. 16 func Trim(str string, characterMask ...string) string { 17 return utils.Trim(str, characterMask...) 18 } 19 20 // TrimStr strips all the given <cut> string from the beginning and end of a string. 21 // Note that it does not strip the whitespaces of its beginning or end. 22 func TrimStr(str string, cut string, count ...int) string { 23 return TrimLeftStr(TrimRightStr(str, cut, count...), cut, count...) 24 } 25 26 // TrimLeft strips whitespace (or other characters) from the beginning of a string. 27 func TrimLeft(str string, characterMask ...string) string { 28 trimChars := utils.DefaultTrimChars 29 if len(characterMask) > 0 { 30 trimChars += characterMask[0] 31 } 32 return strings.TrimLeft(str, trimChars) 33 } 34 35 // TrimLeftStr strips all the given <cut> string from the beginning of a string. 36 // Note that it does not strip the whitespaces of its beginning. 37 func TrimLeftStr(str string, cut string, count ...int) string { 38 var ( 39 lenCut = len(cut) 40 cutCount = 0 41 ) 42 for len(str) >= lenCut && str[0:lenCut] == cut { 43 str = str[lenCut:] 44 cutCount++ 45 if len(count) > 0 && count[0] != -1 && cutCount >= count[0] { 46 break 47 } 48 } 49 return str 50 } 51 52 // TrimRight strips whitespace (or other characters) from the end of a string. 53 func TrimRight(str string, characterMask ...string) string { 54 trimChars := utils.DefaultTrimChars 55 if len(characterMask) > 0 { 56 trimChars += characterMask[0] 57 } 58 return strings.TrimRight(str, trimChars) 59 } 60 61 // TrimRightStr strips all the given <cut> string from the end of a string. 62 // Note that it does not strip the whitespaces of its end. 63 func TrimRightStr(str string, cut string, count ...int) string { 64 var ( 65 lenStr = len(str) 66 lenCut = len(cut) 67 cutCount = 0 68 ) 69 for lenStr >= lenCut && str[lenStr-lenCut:lenStr] == cut { 70 lenStr = lenStr - lenCut 71 str = str[:lenStr] 72 cutCount++ 73 if len(count) > 0 && count[0] != -1 && cutCount >= count[0] { 74 break 75 } 76 } 77 return str 78 } 79 80 // TrimAll trims all characters in string `str`. 81 func TrimAll(str string, characterMask ...string) string { 82 trimChars := utils.DefaultTrimChars 83 if len(characterMask) > 0 { 84 trimChars += characterMask[0] 85 } 86 var ( 87 filtered bool 88 slice = make([]rune, 0, len(str)) 89 ) 90 for _, char := range str { 91 filtered = false 92 for _, trimChar := range trimChars { 93 if char == trimChar { 94 filtered = true 95 break 96 } 97 } 98 if !filtered { 99 slice = append(slice, char) 100 } 101 } 102 return string(slice) 103 }