From 9fcf046fb325a63dade6d4aa4550f44e3c096d4b Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 10 May 2023 10:53:17 +0800 Subject: [PATCH] feat: add Trim and SplitAndTrim --- strutil/string.go | 43 ++++++++++++++++++++++++++++++++++ strutil/string_example_test.go | 32 +++++++++++++++++++++++++ strutil/string_test.go | 22 +++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/strutil/string.go b/strutil/string.go index 95e983a..f9ca53e 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -456,3 +456,46 @@ func ReplaceWithMap(str string, replaces map[string]string) string { 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) +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index 7cda3e8..87d49a3 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -539,3 +539,35 @@ func ExampleReplaceWithMap() { // Output: // 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] +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 78a174e..6fc8a35 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -410,3 +410,25 @@ func TestReplaceWithMap(t *testing.T) { assert.Equal(str, "ac ab ab ac") 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) +}