1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add Substring function

This commit is contained in:
dudaodong
2022-12-29 19:55:40 +08:00
parent b5f7b0e670
commit 1dc5e8ac23
3 changed files with 59 additions and 0 deletions

View File

@@ -281,3 +281,27 @@ func SplitEx(s, sep string, removeEmptyString bool) []string {
return ret
}
// Substring returns a substring of the specified length starting at the specified offset position.
func Substring(s string, offset int, length uint) string {
rs := []rune(s)
size := len(rs)
if offset < 0 {
offset = size + offset
if offset < 0 {
offset = 0
}
}
if offset > size {
return ""
}
if length > uint(size)-uint(offset) {
length = uint(size - offset)
}
str := string(rs[offset : offset+int(length)])
return strings.Replace(str, "\x00", "", -1)
}