improve gstr.TrimStr/TrimLeftStr/TrimRightStr

This commit is contained in:
John Guo
2020-12-20 22:02:14 +08:00
parent 7c52a6f9f6
commit c243637d44
2 changed files with 36 additions and 8 deletions

View File

@ -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
}

View File

@ -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("我爱中国人", "我爱中国"), "人")