mirror of
https://gitee.com/johng/gf
synced 2026-07-04 21:03:13 +08:00
improve gstr.TrimStr/TrimLeftStr/TrimRightStr
This commit is contained in:
@ -37,8 +37,8 @@ func Trim(str string, characterMask ...string) string {
|
||||
|
||||
// TrimStr strips all of the given <cut> string from the beginning and end of a string.
|
||||
// Note that it does not strips the whitespaces of its beginning or end.
|
||||
func TrimStr(str string, cut string) string {
|
||||
return TrimLeftStr(TrimRightStr(str, cut), cut)
|
||||
func TrimStr(str string, cut string, count ...int) string {
|
||||
return TrimLeftStr(TrimRightStr(str, cut, count...), cut, count...)
|
||||
}
|
||||
|
||||
// TrimLeft strips whitespace (or other characters) from the beginning of a string.
|
||||
@ -52,10 +52,17 @@ func TrimLeft(str string, characterMask ...string) string {
|
||||
|
||||
// TrimLeftStr strips all of the given <cut> string from the beginning of a string.
|
||||
// Note that it does not strips the whitespaces of its beginning.
|
||||
func TrimLeftStr(str string, cut string) string {
|
||||
var lenCut = len(cut)
|
||||
func TrimLeftStr(str string, cut string, count ...int) string {
|
||||
var (
|
||||
lenCut = len(cut)
|
||||
cutCount = 0
|
||||
)
|
||||
for len(str) >= lenCut && str[0:lenCut] == cut {
|
||||
str = str[lenCut:]
|
||||
cutCount++
|
||||
if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
|
||||
break
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
||||
@ -71,13 +78,19 @@ func TrimRight(str string, characterMask ...string) string {
|
||||
|
||||
// TrimRightStr strips all of the given <cut> string from the end of a string.
|
||||
// Note that it does not strips the whitespaces of its end.
|
||||
func TrimRightStr(str string, cut string) string {
|
||||
var lenStr = len(str)
|
||||
var lenCut = len(cut)
|
||||
func TrimRightStr(str string, cut string, count ...int) string {
|
||||
var (
|
||||
lenStr = len(str)
|
||||
lenCut = len(cut)
|
||||
cutCount = 0
|
||||
)
|
||||
for lenStr >= lenCut && str[lenStr-lenCut:lenStr] == cut {
|
||||
lenStr = lenStr - lenCut
|
||||
str = str[:lenStr]
|
||||
|
||||
cutCount++
|
||||
if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
|
||||
break
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
@ -26,6 +26,11 @@ func Test_TrimStr(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gstr.TrimStr("gogo我爱gogo", "go"), "我爱")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gstr.TrimStr("gogo我爱gogo", "go", 1), "go我爱go")
|
||||
t.Assert(gstr.TrimStr("gogo我爱gogo", "go", 2), "我爱")
|
||||
t.Assert(gstr.TrimStr("gogo我爱gogo", "go", -1), "我爱")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gstr.TrimStr("啊我爱中国人啊", "啊"), "我爱中国人")
|
||||
})
|
||||
@ -43,6 +48,11 @@ func Test_TrimRightStr(t *testing.T) {
|
||||
t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go"), "gogo我爱")
|
||||
t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go我爱gogo"), "go")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go", 1), "gogo我爱go")
|
||||
t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go", 2), "gogo我爱")
|
||||
t.Assert(gstr.TrimRightStr("gogo我爱gogo", "go", -1), "gogo我爱")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gstr.TrimRightStr("我爱中国人", "人"), "我爱中国")
|
||||
t.Assert(gstr.TrimRightStr("我爱中国人", "爱中国人"), "我")
|
||||
@ -61,6 +71,11 @@ func Test_TrimLeftStr(t *testing.T) {
|
||||
t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go"), "我爱gogo")
|
||||
t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "gogo我爱go"), "go")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go", 1), "go我爱gogo")
|
||||
t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go", 2), "我爱gogo")
|
||||
t.Assert(gstr.TrimLeftStr("gogo我爱gogo", "go", -1), "我爱gogo")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(gstr.TrimLeftStr("我爱中国人", "我爱"), "中国人")
|
||||
t.Assert(gstr.TrimLeftStr("我爱中国人", "我爱中国"), "人")
|
||||
|
||||
Reference in New Issue
Block a user