1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-16 10:42:27 +08:00

feat: add SubInBetween

This commit is contained in:
dudaodong
2024-02-06 17:13:00 +08:00
parent 0fc8733915
commit f82f49a4c2
3 changed files with 69 additions and 5 deletions

View File

@@ -58,6 +58,7 @@ import (
- [SplitAndTrim](#SplitAndTrim) - [SplitAndTrim](#SplitAndTrim)
- [HideString](#HideString) - [HideString](#HideString)
- [RemoveWhiteSpace](#RemoveWhiteSpace) - [RemoveWhiteSpace](#RemoveWhiteSpace)
- [SubInBetween](#SubInBetween)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1274,3 +1275,36 @@ func main() {
// hello world // hello world
} }
``` ```
### <span id="SubInBetween">SubInBetween</span>
<p>Return substring between the start and end position(excluded) of source string.</p>
<b>Signature:</b>
```go
func SubInBetween(str string, start string, end string) string
```
<b>Example:</b>
```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
}
```

View File

@@ -58,6 +58,7 @@ import (
- [SplitAndTrim](#SplitAndTrim) - [SplitAndTrim](#SplitAndTrim)
- [HideString](#HideString) - [HideString](#HideString)
- [RemoveWhiteSpace](#RemoveWhiteSpace) - [RemoveWhiteSpace](#RemoveWhiteSpace)
- [SubInBetween](#SubInBetween)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1307,3 +1308,36 @@ func main() {
// hello world // hello world
} }
``` ```
### <span id="SubInBetween">SubInBetween</span>
<p>获取字符串中指定的起始字符串start和终止字符串end直接的子字符串。</p>
<b>函数签名:</b>
```go
func SubInBetween(str string, start string, end string) string
```
<b>示例:</b>
```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
}
```

View File

@@ -5,7 +5,6 @@
package strutil package strutil
import ( import (
"reflect"
"regexp" "regexp"
"strings" "strings"
"unicode" "unicode"
@@ -350,10 +349,7 @@ func RemoveNonPrintable(str string) string {
// StringToBytes converts a string to byte slice without a memory allocation. // StringToBytes converts a string to byte slice without a memory allocation.
func StringToBytes(str string) (b []byte) { func StringToBytes(str string) (b []byte) {
sh := *(*reflect.StringHeader)(unsafe.Pointer(&str)) return *(*[]byte)(unsafe.Pointer(&str))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
return b
} }
// BytesToString converts a byte slice to string without a memory allocation. // BytesToString converts a byte slice to string without a memory allocation.