diff --git a/docs/strutil.md b/docs/strutil.md index c601278..4320748 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -58,6 +58,7 @@ import ( - [HideString](#HideString) - [ContainsAll](#ContainsAll) - [ContainsAny](#ContainsAny) +- [RemoveWhiteSpace](#RemoveWhiteSpace)
@@ -1390,3 +1391,37 @@ func main() { // false } ``` + + +### RemoveWhiteSpace + +Remove whitespace characters from a string. when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space.
+ +Signature: + +```go +func RemoveWhiteSpace(str string, repalceAll bool) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := " hello \r\n \t world" + + result1 := strutil.RemoveWhiteSpace(str, true) + result2 := strutil.RemoveWhiteSpace(str, false) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // helloworld + // hello world +} +``` diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index fb00385..fdf6ae8 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -58,7 +58,7 @@ import ( - [HideString](#HideString) - [ContainsAll](#ContainsAll) - [ContainsAny](#ContainsAny) - +- [RemoveWhiteSpace](#RemoveWhiteSpace) @@ -975,7 +975,7 @@ func main() { fmt.Println(result1) fmt.Println(result2) - + // Output: // hello world // 你好😄 @@ -1179,7 +1179,6 @@ func main() { } ``` - ### ReplaceWithMap返回`str`的副本,以无序的方式被map替换,区分大小写。
@@ -1250,7 +1249,6 @@ func main() { } ``` - ### SplitAndTrim将字符串str按字符串delimiter拆分为一个切片,并对该数组的每个元素调用Trim。忽略Trim后为空的元素。
@@ -1284,7 +1282,6 @@ func main() { } ``` - ### HideString使用参数`replaceChar`隐藏源字符串中的一些字符。替换范围是 origin[start : end]。
@@ -1392,3 +1389,36 @@ func main() { // false } ``` + +### RemoveWhiteSpace + +删除字符串中的空格,当设置repalceAll为true时,删除全部空格,为false时,替换多个空格为1个空格。
+ +函数签名: + +```go +func RemoveWhiteSpace(str string, repalceAll bool) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := " hello \r\n \t world" + + result1 := strutil.RemoveWhiteSpace(str, true) + result2 := strutil.RemoveWhiteSpace(str, false) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // helloworld + // hello world +} +```