diff --git a/docs/strutil.md b/docs/strutil.md index 6821a12..67781c1 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -45,6 +45,7 @@ import ( - [Unwrap](#Unwrap) - [SplitWords](#SplitWords) - [WordCount](#WordCount) +- [RemoveNonPrintable](#RemoveNonPrintable)
@@ -937,4 +938,35 @@ func main() { // 0 // 0 } +``` + + +### RemoveNonPrintable + +Remove non-printable characters from a string.
+ +Signature: + +```go +func RemoveNonPrintable(str string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n") + result2 := strutil.RemoveNonPrintable("你好😄") + + fmt.Println(result1) + fmt.Println(result2) + // Output: + // hello world + // 你好😄 +} ``` \ No newline at end of file diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index dce5097..6853eaf 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -45,6 +45,7 @@ import ( - [Unwrap](#Unwrap) - [SplitWords](#SplitWords) - [WordCount](#WordCount) +- [RemoveNonPrintable](#RemoveNonPrintable) @@ -936,4 +937,35 @@ func main() { // 0 // 0 } +``` + + +### RemoveNonPrintable + +删除字符串中不可打印的字符。
+ +函数签名: + +```go +func RemoveNonPrintable(str string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := strutil.RemoveNonPrintable("hello\u00a0 \u200bworld\n") + result2 := strutil.RemoveNonPrintable("你好😄") + + fmt.Println(result1) + fmt.Println(result2) + // Output: + // hello world + // 你好😄 +} ``` \ No newline at end of file diff --git a/strutil/string.go b/strutil/string.go index 19b7861..5427812 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -360,3 +360,16 @@ func WordCount(s string) int { return count } + +// RemoveNonPrintable remove non-printable characters from a string. +// Play: todo +func RemoveNonPrintable(str string) string { + result := strings.Map(func(r rune) rune { + if unicode.IsPrint(r) { + return r + } + return -1 + }, str) + + return result +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index d679e13..d0edece 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -438,3 +438,14 @@ func ExampleWordCount() { // 0 // 0 } + +func ExampleRemoveNonPrintable() { + result1 := RemoveNonPrintable("hello\u00a0 \u200bworld\n") + result2 := RemoveNonPrintable("你好😄") + + fmt.Println(result1) + fmt.Println(result2) + // Output: + // hello world + // 你好😄 +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 9867d17..2f90a5f 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -342,3 +342,10 @@ func TestWordCount(t *testing.T) { assert.Equal(v, WordCount(k)) } } + +func TestRemoveNonPrintable(t *testing.T) { + assert := internal.NewAssert(t, "TestRemoveNonPrintable") + + assert.Equal("hello world", RemoveNonPrintable("hello\u00a0 \u200bworld\n")) + assert.Equal("你好😄", RemoveNonPrintable("你好😄")) +}