mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add SubInBetween
This commit is contained in:
@@ -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 ""
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user