mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat: add RemoveNonPrintable
This commit is contained in:
@@ -45,6 +45,7 @@ import (
|
|||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
- [SplitWords](#SplitWords)
|
- [SplitWords](#SplitWords)
|
||||||
- [WordCount](#WordCount)
|
- [WordCount](#WordCount)
|
||||||
|
- [RemoveNonPrintable](#RemoveNonPrintable)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -937,4 +938,35 @@ func main() {
|
|||||||
// 0
|
// 0
|
||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
||||||
|
|
||||||
|
<p>Remove non-printable characters from a string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RemoveNonPrintable(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
// 你好😄
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -45,6 +45,7 @@ import (
|
|||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
- [SplitWords](#SplitWords)
|
- [SplitWords](#SplitWords)
|
||||||
- [WordCount](#WordCount)
|
- [WordCount](#WordCount)
|
||||||
|
- [RemoveNonPrintable](#RemoveNonPrintable)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -936,4 +937,35 @@ func main() {
|
|||||||
// 0
|
// 0
|
||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RemoveNonPrintable">RemoveNonPrintable</span>
|
||||||
|
|
||||||
|
<p>删除字符串中不可打印的字符。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RemoveNonPrintable(str string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```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
|
||||||
|
// 你好😄
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -360,3 +360,16 @@ func WordCount(s string) int {
|
|||||||
|
|
||||||
return count
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -438,3 +438,14 @@ func ExampleWordCount() {
|
|||||||
// 0
|
// 0
|
||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleRemoveNonPrintable() {
|
||||||
|
result1 := RemoveNonPrintable("hello\u00a0 \u200bworld\n")
|
||||||
|
result2 := RemoveNonPrintable("你好😄")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
// Output:
|
||||||
|
// hello world
|
||||||
|
// 你好😄
|
||||||
|
}
|
||||||
|
|||||||
@@ -342,3 +342,10 @@ func TestWordCount(t *testing.T) {
|
|||||||
assert.Equal(v, WordCount(k))
|
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("你好😄"))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user