Add: StringUtils.Substring
This commit is contained in:
@@ -33,3 +33,36 @@ func (s *StringUtils) LeftPad(str string, size int, padStr string) string {
|
||||
|
||||
return strings.Repeat(padStr, padCount) + str
|
||||
}
|
||||
|
||||
func (s *StringUtils) Substring(str string, start, end int) string {
|
||||
if str == "" {
|
||||
return str
|
||||
}
|
||||
|
||||
runes := []rune(str)
|
||||
|
||||
if end < 0 {
|
||||
end = len(runes) + end
|
||||
}
|
||||
if start < 0 {
|
||||
start = len(runes) + start
|
||||
}
|
||||
|
||||
if end > len(str) {
|
||||
end = len(str)
|
||||
}
|
||||
|
||||
if start > end {
|
||||
return EMPTY
|
||||
}
|
||||
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
|
||||
if end < 0 {
|
||||
end = 0
|
||||
}
|
||||
|
||||
return string(runes[start:end])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user