diff --git a/docs/strutil.md b/docs/strutil.md index c086bfa..9ff658e 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -58,6 +58,7 @@ import ( - [SplitAndTrim](#SplitAndTrim) - [HideString](#HideString) - [RemoveWhiteSpace](#RemoveWhiteSpace) +- [SubInBetween](#SubInBetween)
@@ -1274,3 +1275,36 @@ 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/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/strutil_zh-CN.md b/docs/strutil_zh-CN.md index de90f24..9a1a118 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -58,6 +58,7 @@ import ( - [SplitAndTrim](#SplitAndTrim) - [HideString](#HideString) - [RemoveWhiteSpace](#RemoveWhiteSpace) +- [SubInBetween](#SubInBetween) @@ -1307,3 +1308,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/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 +} +``` diff --git a/strutil/string.go b/strutil/string.go index 2498345..54f3b8a 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -5,7 +5,6 @@ package strutil import ( - "reflect" "regexp" "strings" "unicode" @@ -350,10 +349,7 @@ func RemoveNonPrintable(str string) string { // StringToBytes converts a string to byte slice without a memory allocation. func StringToBytes(str string) (b []byte) { - sh := *(*reflect.StringHeader)(unsafe.Pointer(&str)) - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len - return b + return *(*[]byte)(unsafe.Pointer(&str)) } // BytesToString converts a byte slice to string without a memory allocation.