mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 18:22:27 +08:00
feat: add Trim and SplitAndTrim
This commit is contained in:
@@ -456,3 +456,46 @@ func ReplaceWithMap(str string, replaces map[string]string) string {
|
|||||||
|
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SplitAndTrim splits string `str` by a string `delimiter` to an array,
|
||||||
|
// and calls Trim to every element of this array. It ignores the elements
|
||||||
|
// which are empty after Trim.
|
||||||
|
func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
|
||||||
|
result := make([]string, 0)
|
||||||
|
|
||||||
|
for _, v := range strings.Split(str, delimiter) {
|
||||||
|
v = Trim(v, characterMask...)
|
||||||
|
if v != "" {
|
||||||
|
result = append(result, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultTrimChars are the characters which are stripped by Trim* functions in default.
|
||||||
|
DefaultTrimChars = string([]byte{
|
||||||
|
'\t', // Tab.
|
||||||
|
'\v', // Vertical tab.
|
||||||
|
'\n', // New line (line feed).
|
||||||
|
'\r', // Carriage return.
|
||||||
|
'\f', // New page.
|
||||||
|
' ', // Ordinary space.
|
||||||
|
0x00, // NUL-byte.
|
||||||
|
0x85, // Delete.
|
||||||
|
0xA0, // Non-breaking space.
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
// Trim strips whitespace (or other characters) from the beginning and end of a string.
|
||||||
|
// The optional parameter `characterMask` specifies the additional stripped characters.
|
||||||
|
func Trim(str string, characterMask ...string) string {
|
||||||
|
trimChars := DefaultTrimChars
|
||||||
|
|
||||||
|
if len(characterMask) > 0 {
|
||||||
|
trimChars += characterMask[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Trim(str, trimChars)
|
||||||
|
}
|
||||||
|
|||||||
@@ -539,3 +539,35 @@ func ExampleReplaceWithMap() {
|
|||||||
// Output:
|
// Output:
|
||||||
// 1c 12 12 1c
|
// 1c 12 12 1c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleTrim() {
|
||||||
|
result1 := Trim("\nabcd")
|
||||||
|
|
||||||
|
str := "$ ab cd $ "
|
||||||
|
|
||||||
|
result2 := Trim(str)
|
||||||
|
result3 := Trim(str, "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abcd
|
||||||
|
// $ ab cd $
|
||||||
|
// ab cd
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleSplitAndTrim() {
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := SplitAndTrim(str, ",")
|
||||||
|
result2 := SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a b c d $1]
|
||||||
|
// [a b c d 1]
|
||||||
|
}
|
||||||
|
|||||||
@@ -410,3 +410,25 @@ func TestReplaceWithMap(t *testing.T) {
|
|||||||
assert.Equal(str, "ac ab ab ac")
|
assert.Equal(str, "ac ab ab ac")
|
||||||
assert.Equal(ReplaceWithMap(str, replaces), "1c 12 12 1c")
|
assert.Equal(ReplaceWithMap(str, replaces), "1c 12 12 1c")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrim(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str1 := "$ ab cd $ "
|
||||||
|
|
||||||
|
assert.Equal("$ ab cd $", Trim(str1))
|
||||||
|
assert.Equal("ab cd", Trim(str1, "$"))
|
||||||
|
assert.Equal("abcd", Trim("\nabcd"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSplitAndTrim(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTrim")
|
||||||
|
|
||||||
|
str := " a,b, c,d,$1 "
|
||||||
|
|
||||||
|
result1 := SplitAndTrim(str, ",")
|
||||||
|
result2 := SplitAndTrim(str, ",", "$")
|
||||||
|
|
||||||
|
assert.Equal([]string{"a", "b", "c", "d", "$1"}, result1)
|
||||||
|
assert.Equal([]string{"a", "b", "c", "d", "1"}, result2)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user