diff --git a/README.md b/README.md index 605b8ed..45b1bec 100644 --- a/README.md +++ b/README.md @@ -1543,6 +1543,8 @@ import "github.com/duke-git/lancet/v2/strutil" - **IsBlank** : checks if a string is whitespace or empty. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsBlank)] [[play](https://go.dev/play/p/6zXRH_c0Qd3)] +- **IsNotBlank** : checks if a string is not whitespace or not empty. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#IsNotBlank)] - **HasPrefixAny** : checks if a string starts with any of an array of specified strings. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#HasPrefixAny)] [[play](https://go.dev/play/p/8UUTl2C5slo)] diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index 6c71ef2..11a9c0e 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -49,6 +49,7 @@ import ( - [StringToBytes](#StringToBytes) - [BytesToString](#BytesToString) - [IsBlank](#IsBlank) +- [IsNotBlank](#IsNotBlank) - [HasPrefixAny](#HasPrefixAny) - [HasSuffixAny](#HasSuffixAny) - [IndexOffset](#IndexOffset) @@ -1075,6 +1076,45 @@ func main() { } ``` +### IsNotBlank + +
Checks if a string is not whitespace or not empty.
+ +Signature: + +```go +func IsNotBlank(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := IsNotBlank("") + result2 := IsNotBlank(" ") + result3 := IsNotBlank("\t\v\f\n") + result4 := IsNotBlank(" 中文") + result5 := IsNotBlank(" world ") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + // Output: + // false + // false + // false + // true + // true +} +``` + ### HasPrefixAny检查字符串是否以指定字符串数组中的任何一个开头。
diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 1e890de..fe416a6 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -49,6 +49,7 @@ import ( - [StringToBytes](#StringToBytes) - [BytesToString](#BytesToString) - [IsBlank](#IsBlank) +- [IsNotBlank](#IsNotBlank) - [HasPrefixAny](#HasPrefixAny) - [HasSuffixAny](#HasSuffixAny) - [IndexOffset](#IndexOffset) @@ -1076,6 +1077,45 @@ func main() { } ``` +### IsNotBlank + +Checks if a string is not whitespace or not empty.
+ +Signature: + +```go +func IsNotBlank(str string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + result1 := IsNotBlank("") + result2 := IsNotBlank(" ") + result3 := IsNotBlank("\t\v\f\n") + result4 := IsNotBlank(" 中文") + result5 := IsNotBlank(" world ") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + // Output: + // false + // false + // false + // true + // true +} +``` + ### HasPrefixAnyChecks if a string starts with any of an array of specified strings.
diff --git a/strutil/string.go b/strutil/string.go index 169fc1d..17dbe07 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -408,6 +408,11 @@ func IsBlank(str string) bool { return true } +// IsNotBlank checks if a string is not whitespace, not empty. +func IsNotBlank(str string) bool { + return !IsBlank(str) +} + // HasPrefixAny check if a string starts with any of a slice of specified strings. // Play: https://go.dev/play/p/8UUTl2C5slo func HasPrefixAny(str string, prefixes []string) bool { diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index 310fac7..586ab97 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -479,6 +479,26 @@ func ExampleIsBlank() { // false } +func ExampleIsNotBlank() { + result1 := IsNotBlank("") + result2 := IsNotBlank(" ") + result3 := IsNotBlank("\t\v\f\n") + result4 := IsNotBlank(" 中文") + result5 := IsNotBlank(" world ") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + // Output: + // false + // false + // false + // true + // true +} + func ExampleHasPrefixAny() { result1 := HasPrefixAny("foo bar", []string{"fo", "xyz", "hello"}) result2 := HasPrefixAny("foo bar", []string{"oom", "world"}) diff --git a/strutil/string_test.go b/strutil/string_test.go index 2586ec7..1947e27 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -427,6 +427,18 @@ func TestIsBlank(t *testing.T) { assert.Equal(IsBlank(" 中文"), false) } +func TestIsNotBlank(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestIsBlank") + assert.Equal(IsNotBlank(""), false) + assert.Equal(IsNotBlank(" "), false) + assert.Equal(IsNotBlank("\t\v\f\n"), false) + + assert.Equal(IsNotBlank(" 中文"), true) + assert.Equal(IsNotBlank(" world "), true) +} + func TestHasPrefixAny(t *testing.T) { t.Parallel()