diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index 3f67c0b..c378ce6 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -60,6 +60,7 @@ import ( - [ContainsAll](#ContainsAll) - [ContainsAny](#ContainsAny) - [RemoveWhiteSpace](#RemoveWhiteSpace) +- [SubInBetween](#SubInBetween)
@@ -1096,10 +1097,10 @@ import ( func main() { result1 := strutil.IsNotBlank("") - result2 := strutil.IsNotBlank(" ") + result2 := strutil.IsNotBlank(" ") result3 := strutil.IsNotBlank("\t\v\f\n") result4 := strutil.IsNotBlank(" 中文") - result5 := strutil.IsNotBlank(" world ") + result5 := strutil.IsNotBlank(" world ") fmt.Println(result1) fmt.Println(result2) @@ -1462,3 +1463,36 @@ func main() { // hello world } ``` + +### SubInBetween + +获取字符串中指定的起始字符串start和终止字符串end直接的子字符串。
+ +函数签名: + +```go +func SubInBetween(str string, start string, end string) string +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := "abcde" + + result1 := strutil.SubInBetween(str, "", "de") + result2 := strutil.SubInBetween(str, "a", "d") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // abc + // bc +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 0aaf422..2a5c1a5 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -1463,3 +1463,37 @@ func main() { // hello world } ``` + + +### SubInBetween + +Return substring between the start and end position(excluded) of source string.
+ +Signature: + +```go +func SubInBetween(str string, start string, end string) string +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + str := "abcde" + + result1 := strutil.SubInBetween(str, "", "de") + result2 := strutil.SubInBetween(str, "a", "d") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // abc + // bc +} +``` \ No newline at end of file diff --git a/strutil/string.go b/strutil/string.go index 770baf0..02a2b2c 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -574,3 +574,15 @@ func RemoveWhiteSpace(str string, repalceAll bool) string { return strings.TrimSpace(str) } + +// SubInBetween return substring between the start and end position(excluded) of source string. +// Play: todo +func SubInBetween(str string, start string, end string) string { + if _, after, ok := strings.Cut(str, start); ok { + if before, _, ok := strings.Cut(after, end); ok { + return before + } + } + + return "" +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index 586ab97..54adfda 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -653,3 +653,17 @@ func ExampleRemoveWhiteSpace() { // helloworld // hello world } + +func ExampleSubInBetween() { + str := "abcde" + + result1 := SubInBetween(str, "", "de") + result2 := SubInBetween(str, "a", "d") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // abc + // bc +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 1947e27..ce39059 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -566,3 +566,15 @@ func TestRemoveWhiteSpace(t *testing.T) { assert.Equal("helloworld", RemoveWhiteSpace(str, true)) assert.Equal("hello world", RemoveWhiteSpace(str, false)) } + +func TestSubInBetween(t *testing.T) { + assert := internal.NewAssert(t, "TestSubInBetween") + + str := "abcde" + + assert.Equal("", SubInBetween(str, "", "")) + assert.Equal("ab", SubInBetween(str, "", "c")) + assert.Equal("bc", SubInBetween(str, "a", "d")) + assert.Equal("", SubInBetween(str, "a", "")) + assert.Equal("", SubInBetween(str, "a", "f")) +}