github.com/sandwich-go/boost@v1.3.29/xstrings/trim.go (about) 1 package xstrings 2 3 import "strings" 4 5 // DefaultTrimChars are the characters which are stripped by Trim* functions in default. 6 var DefaultTrimChars = string([]byte{ 7 '\t', // Tab. 8 '\v', // Vertical tab. 9 '\n', // New line (line feed). 10 '\r', // Carriage return. 11 '\f', // New page. 12 ' ', // Ordinary space. 13 0x00, // NUL-byte. 14 0x85, // Delete. 15 0xA0, // Non-breaking space. 16 }) 17 18 // Trim 扩展strings.Trim功能,提供默认的mask字符 19 func Trim(str string, characterMask ...string) string { 20 trimChars := DefaultTrimChars 21 if len(characterMask) > 0 { 22 trimChars += characterMask[0] 23 } 24 return strings.Trim(str, trimChars) 25 } 26 27 // SplitAndTrim 将给定字符串分割并trim每一个子元素 28 func SplitAndTrim(str, delimiter string, characterMask ...string) []string { 29 ss := strings.Split(str, delimiter) 30 array := make([]string, 0, len(ss)) 31 for _, v := range ss { 32 if len(characterMask) > 0 { 33 v = Trim(v, characterMask...) 34 } 35 if len(v) > 0 { 36 array = append(array, v) 37 } 38 } 39 return array 40 }