From e1fb97095d4eeb90d3004cfb1960833cf86fe939 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 30 May 2023 14:12:56 +0800 Subject: [PATCH] feat: add ReplaceWithMap, Trim, SplitAndTrim, HideString --- docs/strutil.md | 148 +++++++++++++++++++++++++++++++++++++++- docs/strutil_zh-CN.md | 151 ++++++++++++++++++++++++++++++++++++++++- strutil/string.go | 78 +++++++++++++++++++++ strutil/string_test.go | 53 +++++++++++++++ 4 files changed, 427 insertions(+), 3 deletions(-) diff --git a/docs/strutil.md b/docs/strutil.md index 5c27c56..f0256e8 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -51,6 +51,10 @@ import ( - [HasPrefixAny](#HasPrefixAny) - [HasSuffixAny](#HasSuffixAny) - [IndexOffset](#IndexOffset) +- [ReplaceWithMap](#ReplaceWithMap) +- [Trim](#Trim) +- [SplitAndTrim](#SplitAndTrim) +- [HideString](#HideString)
@@ -1021,4 +1025,146 @@ func main() { // -1 // -1 } -``` \ No newline at end of file +``` + +### ReplaceWithMap + +

Returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.

+ +Signature: + +```go +func ReplaceWithMap(str string, replaces map[string]string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "ac ab ab ac" + replaces := map[string]string{ + "a": "1", + "b": "2", + } + + result := strutil.ReplaceWithMap(str, replaces) + + fmt.Println(result) + // Output: + // 1c 12 12 1c +} +``` + +### Trim + +

Strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.

+ +Signature: + +```go +func Trim(str string, characterMask ...string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + result1 := strutil.Trim("\nabcd") + + str := "$ ab cd $ " + + result2 := strutil.Trim(str) + result3 := strutil.Trim(str, "$") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // abcd + // $ ab cd $ + // ab cd +} +``` + +### SplitAndTrim + +

Splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of slice. It ignores the elements which are empty after Trim.

+ +Signature: + +```go +func SplitAndTrim(str, delimiter string, characterMask ...string) []string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := " a,b, c,d,$1 " + + result1 := strutil.SplitAndTrim(str, ",") + result2 := strutil.SplitAndTrim(str, ",", "$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // [a b c d $1] + // [a b c d 1] +} +``` + +### HideString + +

HideString hide some chars in source string with param `replaceChar`. replace range is origin[start : end]. [start, end).

+ +Signature: + +```go +func HideString(origin string, start, end int, replaceChar string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "13242658976" + + result1 := strutil.HideString(str, 3, 3, "*") + result2 := strutil.HideString(str, 3, 4, "*") + result3 := strutil.HideString(str, 3, 7, "*") + result4 := strutil.HideString(str, 7, 11, "*") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // 13242658976 + // 132*2658976 + // 132****8976 + // 1324265**** +} +``` diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index c29ec30..e3bc8f8 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -51,6 +51,10 @@ import ( - [HasPrefixAny](#HasPrefixAny) - [HasSuffixAny](#HasSuffixAny) - [IndexOffset](#IndexOffset) +- [ReplaceWithMap](#ReplaceWithMap) +- [Trim](#Trim) +- [SplitAndTrim](#SplitAndTrim) +- [HideString](#HideString)
@@ -832,7 +836,6 @@ func main() { } ``` - ### RemoveNonPrintable

删除字符串中不可打印的字符。

@@ -1053,4 +1056,148 @@ func main() { // -1 // -1 } -``` \ No newline at end of file +``` + +### ReplaceWithMap + +

返回`str`的副本,以无序的方式被map替换,区分大小写。

+ +函数签名: + +```go +func ReplaceWithMap(str string, replaces map[string]string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "ac ab ab ac" + replaces := map[string]string{ + "a": "1", + "b": "2", + } + + result := strutil.ReplaceWithMap(str, replaces) + + fmt.Println(result) + // Output: + // 1c 12 12 1c +} +``` + +### Trim + +

从字符串的开头和结尾去除空格(或其他字符)。 可选参数 characterMask 指定额外的剥离字符。

+ +函数签名: + +```go +func Trim(str string, characterMask ...string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + result1 := strutil.Trim("\nabcd") + + str := "$ ab cd $ " + + result2 := strutil.Trim(str) + result3 := strutil.Trim(str, "$") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // abcd + // $ ab cd $ + // ab cd +} +``` + + +### SplitAndTrim + +

将字符串str按字符串delimiter拆分为一个切片,并对该数组的每个元素调用Trim。忽略Trim后为空的元素。

+ +函数签名: + +```go +func SplitAndTrim(str, delimiter string, characterMask ...string) []string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := " a,b, c,d,$1 " + + result1 := strutil.SplitAndTrim(str, ",") + result2 := strutil.SplitAndTrim(str, ",", "$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // [a b c d $1] + // [a b c d 1] +} +``` + + +### HideString + +

使用参数`replaceChar`隐藏源字符串中的一些字符。替换范围是 origin[start : end]。

+ +函数签名: + +```go +func HideString(origin string, start, end int, replaceChar string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/strutil" +) + +func main() { + str := "13242658976" + + result1 := strutil.HideString(str, 3, 3, "*") + result2 := strutil.HideString(str, 3, 4, "*") + result3 := strutil.HideString(str, 3, 7, "*") + result4 := strutil.HideString(str, 7, 11, "*") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // 13242658976 + // 132*2658976 + // 132****8976 + // 1324265**** +} +``` diff --git a/strutil/string.go b/strutil/string.go index 4052925..efd15e8 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -403,3 +403,81 @@ func IndexOffset(str string, substr string, idxFrom int) int { return strings.Index(str[idxFrom:], substr) + idxFrom } + +// ReplaceWithMap returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively. +func ReplaceWithMap(str string, replaces map[string]string) string { + for k, v := range replaces { + str = strings.ReplaceAll(str, k, v) + } + + return str +} + +// SplitAndTrim splits string `str` by a string `delimiter` to a slice, +// and calls Trim to every element of this slice. 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) +} + +// HideString hide some chars in source string with param `replaceChar`. +// replace range is origin[start : end]. [start, end) +func HideString(origin string, start, end int, replaceChar string) string { + size := len(origin) + + if start > size-1 || start < 0 || end < 0 || start > end { + return origin + } + + if end > size { + end = size + } + + if replaceChar == "" { + return origin + } + + startStr := origin[0:start] + endStr := origin[end:size] + + replaceSize := end - start + replaceStr := strings.Repeat(replaceChar, replaceSize) + + return startStr + replaceStr + endStr +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 9b79eca..122234b 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -386,3 +386,56 @@ func TestIndexOffset(t *testing.T) { assert.Equal(IndexOffset(str, "d", len(str)), -1) assert.Equal(IndexOffset(str, "f", -1), -1) } + +func TestReplaceWithMap(t *testing.T) { + assert := internal.NewAssert(t, "TestReplaceWithMap") + + str := "ac ab ab ac" + replaces := map[string]string{ + "a": "1", + "b": "2", + } + + 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) +} + +func TestHideString(t *testing.T) { + assert := internal.NewAssert(t, "TestTrim") + + str := "13242658976" + + assert.Equal("13242658976", HideString(str, 0, -1, "*")) + assert.Equal("13242658976", HideString(str, 0, 0, "*")) + assert.Equal("****2658976", HideString(str, 0, 4, "*")) + + assert.Equal("13242658976", HideString(str, 3, 3, "*")) + assert.Equal("132*2658976", HideString(str, 3, 4, "*")) + assert.Equal("132****8976", HideString(str, 3, 7, "*")) + assert.Equal("1324265****", HideString(str, 7, 11, "*")) + + assert.Equal("1324265****", HideString(str, 7, 100, "*")) + assert.Equal("13242658976", HideString(str, 100, 100, "*")) +}