feat: add Substring function

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

No files matched your search

+24
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)
}